屏幕旋转后的nullPointerException

Err*_*ror 4 android nullpointerexception screen-rotation android-linearlayout

LinearLayout在屏幕旋转后返回对象返回的nullPointerException异常, 虽然我在onCreate()super.onCreate(null)中传递null ; .我知道Activity必须被销毁并重新创建旁边我传递的savedInstanceState = null表示Activity应该在旋转后开始,因为它是第一次启动,为什么我在轮换后得到这个Exception?

onCreate()片段,其中LinearLayout对象名为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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

小智 9

旋转设备时,活动及其上的所有碎片都将被销毁.要避免这种情况,您应该修改AndroidManifest.xml

<activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
Run Code Online (Sandbox Code Playgroud)

这样可以防止异常