Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nullPointerException after Screen rotation

I get nullPointerException exception returned by object LinearLayout after screen rotation although i am passing null in onCreate() super.onCreate(null); . I know Activity must be destroyed and re-created beside i'm passing savedInstanceState = null that mean Activity should start after rotation as it start for first time, why i get this Exception after rotation ?

onCreate() snippet where LinearLayout object called historyText

LinearLayout historyText ;  // this return exception if it used after rotation . 

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    setContentView(R.layout.activity_main);
    historyText = (LinearLayout) findViewById(R.id.historyLayoutText); 
    Log.e("HISTORYVISIBILITY", "VISIBILITY = "+historyText.getVisibility());
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    database = new Database(this);
    Bundle extras = getIntent().getExtras();
    intent = extras.getInt("activity");
    p = new Progress();
    getSupportFragmentManager().beginTransaction()
                .add(R.id.group, p)
                .commit();
    orientation = getRotation();
    switch (orientation){
        case "p" :
            addPlus();
    }
}

Logcat

    10-07 22:21:24.365: E/AndroidRuntime(7768): FATAL EXCEPTION: main
10-07 22:21:24.365: E/AndroidRuntime(7768): Process: developer.mohab.gymee, PID: 7768
10-07 22:21:24.365: E/AndroidRuntime(7768): java.lang.RuntimeException: Unable to start activity ComponentInfo{developer.mohab.gymee/developer.mohab.gymee.Cardio.Cardio}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.LinearLayout.getVisibility()' on a null object reference
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4053)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread.access$900(ActivityThread.java:156)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1357)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.os.Looper.loop(Looper.java:211)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread.main(ActivityThread.java:5389)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at java.lang.reflect.Method.invoke(Native Method)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at java.lang.reflect.Method.invoke(Method.java:372)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
10-07 22:21:24.365: E/AndroidRuntime(7768): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.LinearLayout.getVisibility()' on a null object reference
10-07 22:21:24.365: E/AndroidRuntime(7768):     at developer.mohab.gymee.Cardio.Cardio.onCreate(Cardio.java:76)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.Activity.performCreate(Activity.java:5990)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
10-07 22:21:24.365: E/AndroidRuntime(7768):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
10-07 22:21:24.365: E/AndroidRuntime(7768):     ... 11 more
10-07 22:21:24.633: E/SurfaceFlinger(319): rejecting buffer: bufWidth=1358, bufHeight=624, front.active.{w=193, h=193}
like image 609
Error Avatar asked Dec 25 '22 13:12

Error


1 Answers

When you rotate the device the Activity and all the fragments on it are destroyed. to avoid this you should modify the AndroidManifest.xml

<activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"

this will prevent the Exception

like image 128
TitusCln Avatar answered Jan 11 '23 15:01

TitusCln