1

我得到nullPointerException在屏幕旋轉後由對象LinearLayout返回的異常雖然我在onCreate()中傳遞null null.onCreate(null); 。 我知道活動必須銷燬,並重新創建旁邊我傳遞savedInstanceState =空的意思活動應該開始後輪轉,因爲它第一次啓動,爲什麼我得到這個異常後輪換?nullPointerException在屏幕旋轉後

的onCreate()代碼段,其中稱爲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} 
+2

不要用空參數或任何參數 – Naveed

+1

調用super.onCreate(),如果我沒有片段的onCreate添加( )再次重新創建 – Error

+1

然後不要添加片段,如果它已經存在。看看它是否存在,並使用現有的實例,而不是創建一個新的實例。 – CommonsWare

回答

6

當旋轉裝置中的活性和其上的所有片段被破壞LinearLayout對象。爲了避免這一點,你應該修改的AndroidManifest.xml

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

這樣可以防止異常