切换活动时出现Logcat错误导致应用程序在按下按钮后崩溃

Posted

技术标签:

【中文标题】切换活动时出现Logcat错误导致应用程序在按下按钮后崩溃【英文标题】:Logcat error when switching activity causes app to crash after pressing a button 【发布时间】:2019-02-24 00:38:44 【问题描述】:

我创建了一个包含 2 个活动的应用程序,但是当我按下按钮进入第二个活动时,应用程序崩溃了。我在这里查看了很多问题并在谷歌上搜索了问题,但我没有找到任何解决方案。我查看了 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_
android:layout_
tools:context=".MainActivity">


<Button
    android:id="@+id/necarbogazificat"
    android:layout_
    android:layout_
    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_
    android:layout_
    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_
    android:layout_
    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 ComponentInfocom.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) 

【问题讨论】:

findViewById(R.id.rasplata); 应该在sercontentview 之后完成 成功了!非常感谢! 【参考方案1】:

您必须在onCreate 中调用findViewById,而不是在初始化字段时。 (当 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);
    


【讨论】:

以上是关于切换活动时出现Logcat错误导致应用程序在按下按钮后崩溃的主要内容,如果未能解决你的问题,请参考以下文章

( Android ) 切换到新活动时出现黑屏

快速切换到 UITableViewController 时出现黑屏

应用程序在按下后退键时强制关闭

应用启动时出现异常

将 Delegate 分配给 PopoverPresentationController 时出现 Nil 错误

qt 信号导致连接时出现分段错误