gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:47:54 25 4
gpt4 key购买 nike

我创建了一个具有 2 个 Activity 的应用程序,但是当我按下按钮进入第二个 Activity 时,应用程序崩溃了。我在这里看了很多问题,并在谷歌上搜索了这个问题,但没有找到任何解决方案。我查看了 Logcat,它说它创建了一个 null 异常,但我不知道为什么以及在哪里发生这种情况。

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);
}
});
}

}

主要 Activity 布局

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

第二个Activity 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) 

最佳答案

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

建议的替代第二个 Activity 代码:

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);
}

}

关于java - 切换 Activity 时出现 Logcat 错误导致应用程序在按下按钮后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52412611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com