简体   繁体   中英

App crash on setContentView() of second activity

I have the launch activity called MainActivity that will eventually start another called SearchResultsActivity .

They both extend ActionBarActivity , and the second is yet to be written, for now it is just a stub with:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.search_results);
}

The layout XML is as follows, just a ListView in a white background:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_products"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

The app is crashing on setContentView() with the following logcat:

17348-17348/com.example.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SearchResultsActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
            at android.app.ActivityThread.access$1500(ActivityThread.java:121)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3714)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.example.myapp.SearchResultsActivity.onCreate(SearchResultsActivity.java:42)     <-- this is setContentView()
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1630)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1682)
            at android.app.ActivityThread.access$1500(ActivityThread.java:121)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:940)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3714)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
            at dalvik.system.NativeStart.main(Native Method)

Here's the manifest excerpt:

<activity
    android:name="com.example.myapp.MainActivity"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Styled">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name="com.example.myapp.SearchResultsActivity"
    android:label="@string/app_name"
    android:theme="@style/Theme.Styled"
    android:configChanges="orientation|keyboardHidden">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myapp.MainActivity" />
</activity>

I thought it could be some issue with the layout, but then I decide to replace R.layout.search_results with R.layout.home , which is the same XML used in MainActivity , therefore should work. But it crashes in the very same line.

Any ideas on what could be causing this?

As others have pointed out, your error is likely in your onCreate code for your second activity. If you read the documentation here you'll see they start their activity off with onCreate and an immediate call back to super.onCreate(...)

Your code should be as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); //added this line
    setContentView(R.layout.search_results);
}

If you chose to have your IDE automatically generate an Activity stub, it will always place the call to super.onCreate in your activity. It initializes the activity, see this question for more of an explanation.

If this doesn't fix your problem and you have more code in your second activity, it might be useful to post code for the entire activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM