在片段中使用SharedPreferences时获取NullPointerException
我正在使用TabLayout,并且我有3个片段。当我尝试从SharedPreferences获取值时,第一个Fragment中出现NullPointerException。 Context对象有什么问题吗?请帮忙。感谢 logcat的:在片段中使用SharedPreferences时获取NullPointerException
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()' on a
null object reference
/me.karolis.notsecretproject W/System.err: at android.widget.Toast.<init>
(Toast.java:102)
/me.karolis.notsecretproject W/System.err: at
android.widget.Toast.makeText(Toast.java:260)
/me.karolis.notsecretproject W/System.err: at
me.karolis.notsecretproject.fragments.MainFragment$1.onItemClick
(MainFragment.java:63)me.karolis.notsecretproject.fragments.
MainFragment$1.onItemClick(MainFragment.java:63)
我的偏好帮助器类:
public class PreferencesHelper {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public PreferencesHelper(Context context) {
this.sharedPreferences = context.getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
this.editor = sharedPreferences.edit();
}
public boolean getIsDoingChallenge() {
return sharedPreferences.getBoolean("isDoingChallenge", false);
}
public void setIsDoingChallenge(boolean tof) {
editor.putBoolean("isDoingChallenge", tof).apply();
}
}
第一个片段(其中的问题是):
public class MainFragment extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
listView = (ListView) view.findViewById(R.id.fragment_main_list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
DatabaseHandler db = new DatabaseHandler(getContext().getApplicationContext());
PreferencesHelper preferenceHelper = new PreferencesHelper(getContext().getApplicationContext());
boolean tof = preferenceHelper.getIsDoingChallenge();
Toast.makeText(mainActivity, "" + tof, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
return view;
}
数据库(这是我创建了我的SharedPreferences):
public class DatabaseHandler extends SQLiteOpenHelper {
@Override
public void onCreate(SQLiteDatabase database) {
SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("isDoingChallenge", false).apply();
}
尝试改变:editor.putBoolean("isDoingChallenge", false).apply();
有:editor.putBoolean("isDoingChallenge", false).commit();
问题是与你的mainActivity
变量 - 它为空。
logcat显示您正尝试创建Toast
,因为上下文为空,因此无法使用getResources()
。
如果您致电getActivity()
而不是使用mainActivity
- 它应该修复崩溃。
非常感谢。这有助于解决一个问题。但也许你知道我是如何获得mainActivity不为null的。因为我有一个方法可以在我的片段中使用。 –
如果您想为mainActivity设置一个变量 - 应该在您的Fragment的onCreateView方法(mainActivity = getActivity())中执行此操作。虽然我的首选是总是调用getActivity()而不是保留一个变量。 –
您正在通过构造函数getContext().getApplicationContext()
到PreferencesHelper
构造函数并再次调用context.getApplicationContext()
。
尝试更改PreferencesHelper
构造函数,如下所示。
public PreferencesHelper(Context context) {
this.sharedPreferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
this.editor = sharedPreferences.edit();
}
这不是问题,但谢谢。我修好了它。 –
我可以知道,这是什么问题。 – codeWorm
我使用了一个活动的变量,其中片段是(MainActivity)以使用其中的方法。但后来我改变它((MainActivity)getActivity())。methodName ..它工作 –
把一个破发点上这一行:'Toast.makeText(mainActivity “” + TOF,Toast.LENGTH_SHORT).show();',然后检查'mainActivity',以确保它不为空。 – CzarMatt
@KarolisKursevičius这不是问题,但您不需要执行'getContext()。getApplicationContext()'来获取上下文。只要调用'getContext()'就可以了。 – Eselfar
可能的重复[什么是NullPointerException,以及如何解决它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – Henry