如何对抗. lang。运行时异常:无法启动活动组件信息

时间:2021-08-19 00:38:16

I am currently learning the Java Android SDK. I was trying to get a result from a child activity, and I can't get the setResult() method to work. Every time I start the activity, it doesn't seem to work. How do I use the debugger to locate this issue?

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

Here is the OtherActivity

这是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);


            }

        });

I often run into problems with the Manifest, so if this is part of the issue, here it is.

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

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

Here is activity_second.xml

这是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 个解决方案

#1


0  

Add a check for null intent:

添加空意图检查:

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

#1


0  

Add a check for null intent:

添加空意图检查:

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