gpt4 book ai didi

android - 屏幕旋转后出现 nullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:34 25 4
gpt4 key购买 nike

尽管我在 onCreate() super.onCreate(null) 中传递了 null,但在屏幕旋转后我得到对象 LinearLayout 返回的 nullPointerException 异常; .我知道 Activity 必须在我传递 savedInstanceState = null 的旁边被销毁并重新创建,这意味着 Activity 应该在旋转后启动,因为它是第一次启动,为什么我在旋转后得到这个异常?

onCreate() fragment ,其中 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();
}
}

逻辑猫

    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}

最佳答案

当您旋转设备时,Activity 及其上的所有 fragment 都会被销毁。为避免这种情况,您应该修改 AndroidManifest.xml

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

这将防止异常

关于android - 屏幕旋转后出现 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33001590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com