Android 不幸的应用程序崩溃

Android 不幸的应用程序崩溃,android,android-studio,Android,Android Studio,我在android studio 3.1.3中创建了3个新项目。问题是,每次我将setonclick添加到按钮或手动添加任何视图时,应用程序都会崩溃,这是我创建的程序之一 这是我的MAIN.JAVA文件 public class MainActivity extends AppCompatActivity { Button btnRun, btnClear; EditText txtInput, txtOutput; @Override protected void onCreate(Bund

我在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");
这是我的ACTIVITY.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,因为您的所有视图都在活动中,并且您在初始化后正在设置它。

Do
findViewById
inside
onCreate()
methodafter
setContentView()
Put setContentView(R.layout.activity\u main);仅在super.onCreate(savedInstanceState)之后;让一切都在setContentView方法之后,你能解释一下这是如何解决OP的问题的吗?我知道您已经移动了
setContentView()
方法,但将来阅读此答案的其他人可能不会立即看到。添加“这是更改”仍然无法解释为什么进行此更改可以解决此问题。所需要的只是一点解释,比如“你在膨胀布局之前试图找到你的视图。先膨胀布局”或类似的话。这是一个有效的答案,但不是我想投的答案。