切换活动时,Logcat错误导致按下按钮后应用崩溃

时间:2018-09-19 19:01:09

标签: java android android-activity nullpointerexception crash

我创建了一个包含2个活动的应用程序,但是当我按下按钮进入第二个活动时,该应用程序崩溃了。我在这里查看了许多问题,并在Google上搜索了该问题,但没有找到任何解决方案。我看了一下Logcat,它说它创建了一个空异常,但我不知道发生这种情况的原因和原因。

MainActivity Java代码

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText marius;
    final Button maria;
     final TextView magdalena;
    marius = findViewById(R.id.cascarabete);
    maria = findViewById(R.id.necarbogazificat);
    magdalena = findViewById(R.id.sternocleidomastoidian);
    maria.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent i = new Intent(getApplicationContext(), activity2.class);
        startActivity(i);
        }
    });
}

}

MainActivity布局

<?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">


<Button
    android:id="@+id/necarbogazificat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="34dp"
    android:text="@string/push"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/cascarabete" />

<EditText
    android:id="@+id/cascarabete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:ems="10"
    android:inputType="textPersonName"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/sternocleidomastoidian"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginStart="15dp"
    android:layout_marginTop="45dp"
    app:layout_constraintStart_toStartOf="@+id/necarbogazificat"
    app:layout_constraintTop_toBottomOf="@+id/necarbogazificat" />
    </android.support.constraint.ConstraintLayout>

第二个活动Java代码

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class activity2 extends Activity {
TextView intrebare = findViewById(R.id.intrebare);
EditText raspuns = findViewById(R.id.raspuns);
Button buton = findViewById(R.id.button);
TextView rasplata = findViewById(R.id.rasplata);
String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout2);


}

}

此外,这也是logcat所说的(红色脚本):

09-19 20:56:31.237 20504-20504/com.example.iulian.ffgerg E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iulian.ffgerg, PID: 20504
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.iulian.ffgerg/com.example.iulian.ffgerg.activity2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2639)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6311)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
    at android.app.Activity.findViewById(Activity.java:2380)
    at com.example.iulian.ffgerg.activity2.<init>(activity2.java:11)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2629)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6311) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

1 个答案:

答案 0 :(得分:1)

您必须在findViewById中调用onCreate,而不是在初始化字段时。 (实例化Activity实例后,尚无关联的Window / view层次结构,这会导致您看到崩溃。)

建议的第二种替代活动代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class activity2 extends Activity {

    TextView intrebare;
    EditText raspuns;
    Button buton;
    TextView rasplata;
    String s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);

        intrebare = findViewById(R.id.intrebare);
        raspuns = findViewById(R.id.raspuns);
        buton = findViewById(R.id.button);
        rasplata = findViewById(R.id.rasplata);
    }

}