codefes
codefes

Reputation: 11

Strange 2 NullPointerException on onCreate of android activity

Please help, I have dig into the onSaveInstanceState things and onConfigChange things, but can't get the picture.

The exception was reported for my published app via Google Play, but I can not reproduce it in my development environment. After tracking, I found there are 2 NullPointerException happened in the onCreate method of PreviewActivity:

Issue 1:

(a). Code in PreviewActivity's onCreate method:

public void onCreate(Bundle savedInstanceState) {
   setContentView(R.layout.logo_preview_layout);
   ...

   mBtnCheckGuess = (Button)findViewById(R.id.btn_check_guess);  
   mVgGuessPanel = (View)findViewById(R.id.layout_guess); // the exception line!

   ...

}

(b). Code in layout xml (only part of it)

<ScrollView 
    android:gravity="center_horizontal" 
    android:orientation="vertical" 
    android:id="@+id/main_layout" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:layout_marginTop="25.0dip" android:layout_marginBottom="10.0dip">
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" android:layout_height="wrap_content">            
        <RelativeLayout
            android:id="@+id/layout_guess"
            android:layout_gravity="center_horizontal" 
            android:layout_width="fill_parent" android:layout_height="wrap_content" 
            android:layout_marginTop="0.0dip" android:layout_marginBottom="0.0dip" 
            android:visibility="visible">

I am totally sure that's no other @id/layout_guess in this layout xml file.

(c). Code in AndroidManifest.xml

    <activity
        android:name=".PreviewActivity" 
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:screenOrientation="portrait">
    </activity>

I have got dozens NullPointerException for this issue, but after I removed this line in AndroidManifest:

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

I have not got the same issue for the new release version app for these 2 days. I totally don't know why...

Issue 2: Background, I want to pass a object from Activity A to Activity B when A starts B, I used Android's Application object to contain this object in its instance variable.

(a)Code for MyApplication:

public class MyApplication extends Application{
Logo mSelectedLogo;

@Override
public void onCreate(){
}

public void setSelectedLogo(Logo logo){
    mSelectedLogo = logo;
}
public Logo getSelectedLogo(){
    return this.mSelectedLogo;
}
}

(b) part code of Activity A

mApplication.setSelectedLogo(logo);
            Intent intent = new Intent(this, ActivityB.class);
            LogoListActivity.this.startActivityForResult(intent, REQUEST_GUESS);

(c) code of Activity B

public void onCreate(Bundle savedInstanceState) {
    ...
    MyApplication mApplication = (MyApplication)this.getApplication();
    mSelectedLogo = mApplication.getSelectedLogo();
    ALog.d(TAG, "correct logo name: " + mSelectedLogo.mName + ", id: " + mSelectedLogo.mId); // NullPointerException line, obviously mApplication.getSelectedLogo() returns NULL
    ...
}

It's really strange why it will be NULL. Unless the MyApplication object has been destroyed.

Error log's here:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codefes.quiz/com.codefes.quiz.PreviewActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) at android.app.ActivityThread.access$1500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3687) 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:867) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.codefes.quiz.PreviewActivity.onCreate(Unknown Source) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) ... 11 more

Please help, thank you!

Upvotes: 0

Views: 955

Answers (1)

Vikram Bodicherla
Vikram Bodicherla

Reputation: 7123

Assume you have a layout file that you have placed in layout-potrait. If the user switches to landscape(If you activity is not marked as potrait-only), then no layout gets set.

So resources of higher order (or just different order in some case) are not used when the device or your application is in a lower order state.

Since you've put in a lot of configChange strings, check if you have layouts catering to all scenarios.

Upvotes: 1

Related Questions