为什么 findViewById() 会导致 relativeLayout 初始化命令崩溃?

why might findViewById() crash relativeLayout init command?

我正在 Android Studio 3.0 中编写一个国际象棋应用程序,我希望在用户单击 btnPopUpWindow 时出现弹出窗口,这是棋盘布局上的一个按钮。这是棋盘布局 XML:

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加载的。显然,Chessboard 将是弹出窗口 window 的父级,这就是为什么我想为弹出窗口使用 RelativeLayout 引用以查看 Chessboard。

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() 调用 returns null,这可不好。具体来说,ActivityThread.java中的performLaunchActivity()抛出了“无法启动activity”异常不知道这是怎么回事。

我已经通读了 StackOverview 搜索中弹出的其他 findViewById() 话题,其中很多问题似乎都围绕 findViewById() 返回的项目与局部变量不匹配 Button x = findViewById(R.id.TextFieldHere); 例如。但这里似乎并非如此……除非我不明白 RelativeLayout 应该如何在这里工作……?任何 advice/insight 不胜感激,谢谢。

您的 R.id.chessboardlayout 是一个 android.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>

然后在你的activity中正常使用findViewById()

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