为什么findViewById()会崩溃relativeLayout init命令?

问题描述:

我正在Android Studio 3.0中编写一个国际象棋应用程序,当用户点击棋盘布局上的按钮btnPopUpWindow时,我想要一个PopupWindow出现。这里”棋盘布局XML:为什么findViewById()会崩溃relativeLayout init命令?

activity_chessboard.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.androidchess.MainActivity" 
    android:id="@+id/chessboardlayout"> 

    <Button 
     android:id="@+id/popUpButton" 
     android:layout_width="172dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginTop="8dp" 
     android:text="Pop Up Menu" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginRight="8dp" /> 

</android.support.constraint.ConstraintLayout> 

的XML布局上述由Chessboard.java加载。显然,棋盘将是弹出窗口的父项,这就是为什么我想使用RelativeLayout引用来弹出窗口来查看棋盘。

Chessboard.java

public class Chessboard extends Activity { 

Button btnPopUpWindow; 
RelativeLayout relativeLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_chessboard); 

    btnPopUpWindow = findViewById(R.id.popUpButton);      // This works 
    relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout); // <--- CRASHES HERE!!! 
    btnPopUpWindow.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // https://www.youtube.com/watch?v=wxqgtEewdfo 

      gameTextArea.setText("Pop up Menu appears..."); 

     } 
    }); 
} 

及其该relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout);线,其崩溃的应用程序。

我假设findViewById()未能找到ID chessboardlayout,但我不明白为什么。我在Android Studio中找不到R.id目录,但我注意到,当我输入命令时,chessboardlayout出现在下拉选项中。另外,当我通过调试器时,我会看到Id。所以我认为Id已经正确提交。

但是,当我按照调试器,findViewById()调用返回null,这不可能是好的。特别是,无法启动活动performLaunchActivity()ActivityThread.java抛出异常不知道这是怎么回事。

我已经通过其他findViewById()线程在StackOverview搜索弹出,很多这些问题看似乎围绕着由findViewById()不匹配的局部变量Button x = findViewById(R.id.TextFieldHere);例如返回的项目。但是,这似乎并不是这种情况...除非我不明白RelativeLayout应该如何在这里工作......?任何意见/见解表示赞赏,谢谢。

您的R.id.chessboardlayoutandroid.support.constraint.ConstraintLayout,因此您不能将它投射到RelativeLayout或它会导致ClassCastException

所以,如果你仍然想使用的RelativeLayout作为布局的根,改变你的activity_chessboard.xml这样:

activity_chessboard.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.androidchess.MainActivity" 
    android:id="@+id/chessboardlayout"> 

    <Button 
     android:id="@+id/popUpButton" 
     android:layout_width="172dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginTop="8dp" 
     android:text="Pop Up Menu" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginRight="8dp" /> 
</RelativeLayout> 

然后在你的活动使用findViewById()正常。

setContentView(R.layout.activity_chessboard); 
btnPopUpWindow = findViewById(R.id.popUpButton);      
relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout);