在Fragment中使用SharedPreferences时获取NullPointerException

时间:2017-07-26 22:49:01

标签: android android-fragments nullpointerexception sharedpreferences

我正在使用TabLayout,我有3个片段。当我尝试从SharedPreferences获取值时,我在第一个Fragment中得到NullPointerException。 Context对象有什么问题吗?请帮忙。谢谢 logcat的:

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();
}

3 个答案:

答案 0 :(得分:0)

尝试更改:editor.putBoolean("isDoingChallenge", false).apply();

with:editor.putBoolean("isDoingChallenge", false).commit();

答案 1 :(得分:0)

问题出在你的> src: > main: > java: > com.example: Application.java > resources: application.properties > test: > java: > com.example: ApplicationTest.java > pom.xml 变量上 - 它是空的。

logcat显示您正在尝试创建一个无法mainActivity的{​​{1}}(因为上下文为空)。

如果您拨打Toast而不是使用getResources(),则应该解决此问题。

答案 2 :(得分:0)

您正在将getContext().getApplicationContext()传递给PreferencesHelper构造函数并再次从构造函数中调用context.getApplicationContext()

尝试更改PreferencesHelper构造函数,如下所示。

public PreferencesHelper(Context context) {
    this.sharedPreferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
    this.editor = sharedPreferences.edit();
}