- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 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 编程的新手,我对 View.findViewById() 和 Activity.findViewById() 很困惑,我们什么时候必须调用这些方法以及在哪个方法中调用类 find
/** * A placeholder fragment containing a simple view. */ public static class Placehol
我尝试在 java 中设置来自 TextView 的文本:首先我创建了一个这样的对象: //Property info TextView propertyInfo = (TextView) info.
[更新] -- 只是想向所有人确认,我同意使用硬编码号码总是一个坏主意。我的好奇心是在使用 Maps API 时枚举的 1、2 常量值是什么。如果我找到像 R.id.my_current_locati
在我的 fragment 的 onCreateView 方法中,我曾经将膨胀的布局存储在一个变量中,然后将后者传递给我的其他函数,这些函数在这个膨胀的 上调用了 findViewById >查看。 现
我正在尝试实现自定义数组适配器, 问题是我的代码在使用时崩溃 ImageView imageView=(ImageView)findViewById(R.id.imageView); 而不是 Ima
我想在多个 Activity 中使用 View 。就我而言,它是一个 FloatingActionButton。当我通过 findViewById 搜索 View 时,程序抛出 NullPointer
findViewById 给出一个错误,我认为这是因为它返回 null。我的应用程序被迫关闭。 SensorManager sensorManager=null; TextView x= null;
我刚刚创建了简单的应用程序并添加了按钮。 MyActivity.java package com.example.myapp; import android.app.Activity; import
我的应用程序在布局中有许多元素(远超过 100 个),并且在 MainActivity 中声明所有元素以便访问它们,这使得我的 MainActivity 类难以阅读和调试。因此,我创建了一个新类来处理
我刚刚开始android开发,我被困在这个: static Handler h = new Handler() { @Override public void handleMessag
我是一名初学者,正在磨练我的应用程序开发技能。我目前正在做一个示例,其中列表由字符串数组填充。在最后的步骤中进行绑定(bind),这是找不到方法“findviewbyid”的地方,并且在构建后显示为红
这是我函数的一部分,android studio 无法识别 findViewById 函数。谁能帮我找出问题所在吗?\ public void onClick(View v) {
我在 LinearLayout 内的相对布局内访问 TextView 时遇到问题 假设我在文件 row.xml 中有这些 View
很抱歉标题含糊不清, 下面是麻烦。我想使用 x 变量来迭代该对象,该对象在名称中使用 1 -9 值 (R.id.imageButtonx) for (int x = 1; x <10; x++)
谁能帮我找出这个程序的问题所在。在 onCreate() 方法中,findViewById() 为所有 id 返回 null,这会导致稍后出现空指针异常。我不明白为什么 findViewById()
我做错了什么,我检查了几次ID,在XML中一切正常,但是当我想构建发布应用程序时,会出现此错误,当我构建调试应用程序时,我不会出现此错误。 error: cannot find symbol
我是 Android 编程新手,所以我有一个简单的问题!我在 activity_main.xml 和 main_activity.java 中创建了一个 button 我想通过调用 findbyvie
我对 Android 开发非常陌生,所以如果我无法理解这里到底发生了什么,我很抱歉。 我创建了一个形状,如下所示。它包含在可绘制文件夹内的 shape.xml 中: 现在在我的 Acti
谁能帮我找出这个程序的问题所在。在 onCreate() 方法中,findViewById() 为所有 id 返回 null,这会导致稍后出现空指针异常。我不明白为什么 findViewById()
我是一名优秀的程序员,十分优秀!