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

标签 java android android-layout android-relativelayout

我正在 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 将是弹出窗口的父级,这就是为什么我想为弹出窗口使用 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 ,但我不明白为什么。我找不到 R.id Android Studio 中的目录,但我注意到 chessboardlayout当我输入命令时出现在下拉选项中。另外,我在调试器中看到了 Id。所以我假设 Id 已正确归档。

但是当我跟随调试器时,findViewById()调用返回 null,这不太好。具体来说,performLaunchActivity() 抛出“无法启动 Activity ”异常在 ActivityThread.java 中不知道这是怎么发生的。

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

最佳答案

你的 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);

关于java - 为什么 findViewById() 会导致 relativeLayout 初始化命令崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47615775/

相关文章:

android - 有没有办法获取Bitmap的存储大小?

java - 隐藏操作栏中的标题并显示图标

java - 如何使用 JiBX 读取 XML 文档?

java - Runtime Exec 似乎忽略了撇号

Android - 尝试使用 mGeoDataClient.getPlaceById(id) 从任务中获取 PlaceBufferResponse,但失败

android - 如何渐变填充按钮的背景?

android - 从 ViewHolder(列表)中删除项目,更新屏幕

java - 如何在TabLayout中包裹单个选项卡宽度?

java - 我如何以编程方式发送图片作为电子邮件的一部分

java - 写 !string.isEmpty() 有什么缺点吗?