android - 不幸的应用程序崩溃
问题描述
我在我的 android studio 3.1.3 中创建了 3 个新项目。问题是每次我将 setonclick 添加到按钮或手动添加应用程序崩溃的任何视图时,这是我创建的程序之一这是我的 MAIN.JAVA 文件
public class MainActivity extends AppCompatActivity {
Button btnRun, btnClear;
EditText txtInput, txtOutput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtInput = findViewById(R.id.txtInput);
txtOutput = findViewById(R.id.txtOutput);
btnClear = findViewById(R.id.btnClear);
btnRun = findViewById(R.id.btnRun);
setContentView(R.layout.activity_main);
/*btnRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Working", Toast.LENGTH_SHORT).show();
}
});*/
txtInput.setText("2");
txtOutput.setText("2 x 1 = 2\n" +
"2 x 2 = 4");
这是我的活动.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=".MainActivity">
<EditText
android:id="@+id/txtOutput"
android:layout_width="221dp"
android:layout_height="0dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintBottom_toTopOf="@+id/btnClear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtInput" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginEnd="29dp"
android:text="@string/clear_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnRun"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtOutput" />
<Button
android:id="@+id/btnRun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="9dp"
android:layout_marginTop="18dp"
android:text="@string/run_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnClear"
app:layout_constraintTop_toBottomOf="@+id/txtOutput" />
<EditText
android:id="@+id/txtInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="27dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/txtOutput"
app:layout_constraintStart_toStartOf="@+id/txtOutput"
app:layout_constraintTop_toTopOf="parent" />
请帮我找出问题但是如果我对“setOnClick”进行评论或评论或取出程序运行的setText
解决方案
尝试这个
public class MainActivity extends AppCompatActivity {
Button btnRun, btnClear;
EditText txtInput, txtOutput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // here is the change
txtInput = findViewById(R.id.txtInput);
txtOutput = findViewById(R.id.txtOutput);
btnClear = findViewById(R.id.btnClear);
btnRun = findViewById(R.id.btnRun);
/*btnRun.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Working", Toast.LENGTH_SHORT).show();
}
});*/
txtInput.setText("2");
txtOutput.setText("2 x 1 = 2\n" +
"2 x 2 = 4");
当您在设置视图之前进行初始化时。它将给出 null 因为您在活动中的所有视图并且您在初始化后设置它。
推荐阅读
- corda - 通过连接两个模式列值并比较来编写 Corda 自定义查询?
- c# - 从用 jquery 填充的 asp.net 下拉列表中获取文本
- c# - 从列表框中获取选中的项目
- excel - 满足条件后如何创建新行?Excel/VBA
- javascript - 根据Javascript中的参数查找嵌套对象
- excel - VBA - 基于数据值的高级宏更改单元格颜色
- sql - 提取时间持续时间的等效 HOUR
- react-native - InputAccessoryView 在键盘关闭后隐藏
- amazon-web-services - 多台服务器上的数据库用于写入和单个服务器上的读取
- flutter - 如何在 Flutter App 上获取编译/构建日期?