gpt4 book ai didi

java - PreferenceManager.getDefaultSharedPreferences 中的空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:31 29 4
gpt4 key购买 nike

这是我制作的应用程序类的代码,我用它来保留领域数据库的默认配置,并保留 2 个静态方法来处理 SharedPreferences,其中一个我得到 NullPointerException

public class BucketDropsApp extends Application {
@Override
public void onCreate() {
super.onCreate();
RealmConfiguration config=new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(config);
}

public static void save(Context context,int state){
SharedPreferences mPrefs= PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor mEditor=mPrefs.edit();
mEditor.putInt("State",state);
mEditor.apply();
}

public static int load(Context context){
SharedPreferences mPrefs= PreferenceManager.getDefaultSharedPreferences(context);
int stateType=mPrefs.getInt("State", StateType.NONE);
return stateType;
}

}

这是 MainActivity 的类:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.main_toolBar);
setSupportActionBar(mToolbar);
mBtnAdd = (Button) findViewById(R.id.btn_trigger_dialog);
mBtnAdd.setOnClickListener(mBtnAddListener);
mRealm = Realm.getDefaultInstance();
int filterOption = BucketDropsApp.load(this);
loadResults(filterOption);
mEmptyView = findViewById(R.id.emptyScreen);
mRecycler = (BucketDropRecycler) findViewById(R.id.rv_drops);
mRecycler.addItemDecoration(new Divider(this, LinearLayoutManager.VERTICAL));
mRecycler.hideIfEmpty(mToolbar);
mRecycler.showIfEmpty(mEmptyView);
mAdapter = new DropsAdapter(this, mResults, mAddListener, mMarkListener,mRealm);
mRecycler.setAdapter(mAdapter);
CustomCallback callback = new CustomCallback(mAdapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(mRecycler);
initBackgroundImage();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
boolean handled = true;
int filterOption = StateType.NONE;
switch (id) {
case R.id.menu_add:
showDialogAdd();
break;
case R.id.menu_least:
filterOption = StateType.LEAST_TIME_LEFT;
break;
case R.id.menu_most:
filterOption = StateType.MOST_TIME_LEFT;
break;
case R.id.menu_complete:
filterOption = StateType.COMPLETE;
break;
case R.id.menu_incomplete:
filterOption = StateType.INCOMPLETE;
break;
default:
handled = false;
break;
}
loadResults(filterOption);
BucketDropsApp.save(this, filterOption);
return handled;
}

private void loadResults(int filterOption) {
switch (filterOption) {
case StateType.NONE:
mResults = mRealm.where(Drop.class).findAllAsync();
break;
case StateType.LEAST_TIME_LEFT:
mResults = mRealm.where(Drop.class).findAllSortedAsync("when");
break;
case StateType.MOST_TIME_LEFT:
mResults = mRealm.where(Drop.class).findAllSortedAsync("when", Sort.DESCENDING);
break;
case StateType.COMPLETE:
mResults = mRealm.where(Drop.class).equalTo("completed", true).findAllAsync();
break;
case StateType.INCOMPLETE:
mResults = mRealm.where(Drop.class).equalTo("completed", false).findAllAsync();
break;
}
mResults.addChangeListener(mChangeListener);
}

这就是当我尝试运行应用程序时 logcat 中弹出的内容

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.compassasu.bucketdrops/com.compassasu.bucketdrops.ActivityMain}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
at com.compassasu.bucketdrops.BucketDropsApp.load(BucketDropsApp.java:33)
at com.compassasu.bucketdrops.Recycler.DropsAdapter.update(DropsAdapter.java:64)
at com.compassasu.bucketdrops.Recycler.DropsAdapter.<init>(DropsAdapter.java:51)
at com.compassasu.bucketdrops.ActivityMain.onCreate(ActivityMain.java:105)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
at android.app.ActivityThread.access$800(ActivityThread.java:135) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5001) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

您的 Activity 是否扩展了 android.app.Application?如果是,那么您可以尝试按如下方式传递上下文,即代替此传递 getApplicationContext()

mAdapter = new DropsAdapter(getApplicationContext(), mResults,  mAddListener, mMarkListener,mRealm);

可能需要引用以下链接:

Should I use getApplicationContext or Activity.this in a long running AsyncTask

Some notes about Android Context

关于java - PreferenceManager.getDefaultSharedPreferences 中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38206795/

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