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

标签 java android logcat

我目前正在学习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/

相关文章:

java - GlassFish:使用 --force 进行部署

Android strace(或任何 linux 二进制文件)执行

android - 无法在 Android Studio 上设置 Android NDK 版本

android - 找不到从 Google Play Services Util 引用的类

java - 在 Android 中更改按钮单击的布局

java - spring hibernatetemplate createSQLQuery where in(..) 可以多长时间?

java - poi API 创建的 Excel 文件 : data is there but only one row is viewable?

java - Spring Boot 中 Grails 的 BootStrap#init 等效项?

java - 如何打开OTG连接设置

android - 在 Logcat 上显示更多字符串