如何对抗java.lang.RuntimeException:无法启动活动ComponentInfo

时间:2014-08-24 19:12:35

标签: java android logcat

我目前正在学习Java Android SDK。我试图从子活动中获得结果,但我无法使用setResult()方法。每次我开始活动时,它似乎都不起作用。如何使用调试器来查找此问题?

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

这是OtherActivity

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


            }

        });

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

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

1 个答案:

答案 0 :(得分:0)

添加空意图检查:

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