gpt4 book ai didi

java - 如何解决java.lang.RuntimeException : Unable to start activity ComponentInfo

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:44 29 4
gpt4 key购买 nike

我目前正在学习Java Android SDK。我试图从子 Activity 中获取结果,但无法使用 setResult() 方法。每次我开始 Activity 时,似乎都不起作用。如何使用调试器定位这个问题?

08-24 15:00:44.018: E/AndroidRuntime(32400): FATAL EXCEPTION: main
08-24 15:00:44.018: E/AndroidRuntime(32400): Process: com.example.theotherproject, PID: 32400
08-24 15:00:44.018: E/AndroidRuntime(32400): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.theotherproject/com.example.theotherproject.OtherActivity}: java.lang.NullPointerException
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.ActivityThread.access$800(ActivityThread.java:156)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.os.Looper.loop(Looper.java:157)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.ActivityThread.main(ActivityThread.java:5872)
08-24 15:00:44.018: E/AndroidRuntime(32400): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 15:00:44.018: E/AndroidRuntime(32400): at java.lang.reflect.Method.invoke(Method.java:515)
08-24 15:00:44.018: E/AndroidRuntime(32400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-24 15:00:44.018: E/AndroidRuntime(32400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
08-24 15:00:44.018: E/AndroidRuntime(32400): at dalvik.system.NativeStart.main(Native Method)
08-24 15:00:44.018: E/AndroidRuntime(32400): Caused by: java.lang.NullPointerException
08-24 15:00:44.018: E/AndroidRuntime(32400): at com.example.theotherproject.OtherActivity.onCreate(OtherActivity.java:34)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.Activity.performCreate(Activity.java:5312)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
08-24 15:00:44.018: E/AndroidRuntime(32400): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
08-24 15:00:44.018: E/AndroidRuntime(32400): ... 11 more

这是其他 Activity

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

line = (String) getIntent().getSerializableExtra(TAG);

textView = (TextView) findViewById(R.id.textView);
textView.setText(line);

checkBox = (CheckBox) findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


checked = isChecked;
Intent i = new Intent();
i.putExtra(OTHERTAG, checked);
setResult(RESULT_OK, i);


}

});

我经常遇到 list 问题,所以如果这是问题的一部分,那就是这里。

<activity
android:name="com.example.theotherproject.FirstFragment"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OtherActivity"
android:label="@string/app_name">
</activity>

这是activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="157dp"/>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp"
/>

</RelativeLayout>

最佳答案

添加对空 Intent 的检查:

if (getIntent() != null) {
line = (String) getIntent().getSerializableExtra(TAG);
} else {
Log.e("TAG","Intent was null");
}

关于java - 如何解决java.lang.RuntimeException : Unable to start activity ComponentInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475344/

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