gpt4 book ai didi

java - 我的 Activity 中的 onResume() 发生 Android 崩溃 - 为什么?

转载 作者:行者123 更新时间:2023-12-02 17:50:48 24 4
gpt4 key购买 nike

错误是:

无法恢复 Activity :java.lang.NullPointerException

这是堆栈跟踪:

0 java.lang.RuntimeException: Unable to resume activity {com.sortitapps.movies/com.sortitapps.movies.SettingsActivity}: java.lang.NullPointerException
1 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2944)
2 at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2973)
3 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2408)
4 at android.app.ActivityThread.access$600(ActivityThread.java:165)
5 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
6 at android.os.Handler.dispatchMessage(Handler.java:107)
7 at android.os.Looper.loop(Looper.java:194)
8 at android.app.ActivityThread.main(ActivityThread.java:5391)
9 at java.lang.reflect.Method.invokeNative(Native Method)
10 at java.lang.reflect.Method.invoke(Method.java:525)
11 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
13 at dalvik.system.NativeStart.main(Native Method)
14 Caused by: java.lang.NullPointerException
15 at com.sortitapps.movies.SettingsActivity.onResume(SettingsActivity.java:149)
16 at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
17 at android.app.Activity.performResume(Activity.java:5200)
18 at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2931)

这是代码。 第149行是adapter.notifyDataSetChanged();:

@Override
public void onResume()
{
super.onResume(); // Always call the superclass method first

SharedPreferences settings = getSharedPreferences("prefs", 0);

int numberDesktop = settings.getInt("desktop", 0);
Log.i("Desktop purchased:", String.valueOf(numberDesktop));

int numberScans = settings.getInt("scanning", 0);
Log.i("Number of scans:", String.valueOf(numberScans));

if (settingsitems.contains("Unlock Desktop Apps") &&
numberDesktop == 2)
{
settingsitems.remove("Unlock Desktop Apps");
}

String freeapp = getResources().getString(R.string.free_app);

if (settingsitems.contains("Unlock Premium Features") &&
freeapp.equals("Yes") &&
numberScans > 10)
{
settingsitems.remove("Unlock Premium Features");
}

adapter.notifyDataSetChanged();
}

恢复时是否需要再次初始化适配器?

最佳答案

适配器肯定是空的。

您的代码没有显示适配器是如何初始化的,因此可能是它没有用值初始化,或者之后被设置为 null。

关于java - 我的 Activity 中的 onResume() 发生 Android 崩溃 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220522/

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