gpt4 book ai didi

java - 将额外内容放入 bundle 中会导致奇怪的冲突和应用程序崩溃......为什么?

转载 作者:行者123 更新时间:2023-12-01 09:34:22 25 4
gpt4 key购买 nike

我在主应用程序 Activity 中遇到了一个奇怪的错误。

我的 Activity 中有一个 FragmentStatePagerAdapter (作为内部类),其 instantiateItem(ViewGroup container, intposition) 方法被重写以处理方向更改。

此方法工作正常,直到我通过 onSaveInstanceState(Bundle outState) 将字符串放入 Bundle 中。当像这样实现该方法时,不会调用 instantiateItem:

 @Override
protected void onSaveInstanceState(Bundle outState){
if (myObject != null) {
outState.putString(MY_OBJECT_KEY, myObject.getName());
}
super.onSaveInstanceState(outState);
}

当我删除 outState.putString(MY_OBJECT_KEY, myObject.getName()) 时,每次更改方向时都会毫无问题地调用 instantiateItem。为什么是这样?非常感谢任何帮助。

我得到的错误如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.miki.rpiplantsapp, PID: 22292
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.miki.rpiplantsapp/com.android.miki.rpiplantsapp.PlantStatsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.android.miki.rpiplantsapp.MoistureFragment.setOptimalStatText(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.android.miki.rpiplantsapp.MoistureFragment.setOptimalStatText(java.lang.String)' on a null object reference
at com.android.miki.rpiplantsapp.PlantStatsActivity$ViewPageAdapter.updateCurrentFragsOptimal(PlantStatsActivity.java:733)
at com.android.miki.rpiplantsapp.PlantStatsActivity.loadPlantItemTabs(PlantStatsActivity.java:426)
at com.android.miki.rpiplantsapp.PlantStatsActivity.onCreate(PlantStatsActivity.java:225)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4077) 
at android.app.ActivityThread.-wrap15(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1350) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5422) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

instantiateItem 方法如下所示:

@Override
public Object instantiateItem(ViewGroup container, int position){
switch (position){
case 0:
MoistureFragment moistureTab = (MoistureFragment) super.instantiateItem(container, position);
currentMoistureFragment = moistureTab;
return currentMoistureFragment;

case 1:
LightFragment lightTab = (LightFragment) super.instantiateItem(container, position);
currentLightFragment = lightTab;
return currentLightFragment;
case 2:
TemperatureFragment tempTab = (TemperatureFragment) super.instantiateItem(container, position);
currentTempFragment = tempTab;
return currentTempFragment;
default:
return null;
}
}

updateCurrentFragsOptimal() 看起来像这样:

   public void updateCurrentFragsOptimal(double moistureValue, double lightValue, double tempValue){
currentMoistureFragment.setOptimalStatText(String.valueOf(moistureValue));
currentLightFragment.setOptimalStatText(String.valueOf(lightValue));
currentTempFragment.setOptimalStatText(String.valueOf(tempValue));
}

最佳答案

嗯,这比我想象的要简单...我发现当 savedInstanceState 不为 null 时,我在 onCreate() 方法中调用了一个方法,那调用了另一种方法。此方法做了几件事,阻止了 instantiateItem 被调用,因此 fragment 最终为 null 并导致 NullPointerException。只是一个简单的逻辑错误。

关于java - 将额外内容放入 bundle 中会导致奇怪的冲突和应用程序崩溃......为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39132317/

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