谷歌Play管理中心崩溃报告在活动响应方法中的首选项管理器



Google Play 管理中心崩溃报告错误 :

java.lang.NullPointerException: 
at in.abc.abc.abc.abc.Event_List$2.onResponse (Event_List.java:119)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run (ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback (Handler.java:794)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:176)
at android.app.ActivityThread.main (ActivityThread.java:6635)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:823)

活动活动 :

PrefManager prefManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
prefManager = new PrefManager(Event_List.this);
getAllEvents();
}
private void getAllEvents()
{       
getAllEvents.enqueue(new Callback<Events>() {
@Override
public void onResponse(Call<Events> call, Response<Events> response)
{
if(!response.isSuccessful())
{
if (response.code() == 400 || response.code() == 401 )
{
prefManager.clear(); 
}
Toast.makeText(Event_List.this, "Please try again", Toast.LENGTH_SHORT).show();
return;
}
}
});
}
}

获取 java.lang.NullPointerException for clear((; 方法。

首选项管理器代码 :

public static SharedPreferences sharedPreferences;
public PrefManager(Context context)
{
this.context = context;
sharedPreferences = context.getSharedPreferences(STORE_FILE_NAME, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}   
public void clear()
{
editor.clear();
editor.commit();
}

我在运行时收到谷歌播放管理中心中的首选项管理器此错误。 任何人都可以建议在项目中使用首选项管理器的正确方法吗?

创建类:

public class MySharedPref {
public static SharedPreferences sharedPreferences;
Context context;
SharedPreferences.Editor editor;
public String getCountry() {
return sharedPreferences.getString("COUNTRY","");
}
public void setCountry(String country) {
this.country = country;
insert("COUNTRY",country);
}
// Instantiate constructor
public MySharedPref(Context context)
{
this.context = context;
sharedPreferences = context.getSharedPreferences("MY_PREF", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
public void insert(String key, String value) {
editor.putString(key, value);
editor.apply();
}
public void clear()
{
editor.clear();
editor.commit();
}
}

MainActivity

MySharedPref sharedPref;
String country = "";
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = new MySharedPref(MainActivity.this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
materialButton = findViewById(R.id.materialButton);
if (sharedPref.getCountry()!=null) {
textView.setText(sharedPref.getCountry());
}
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String selection = (String) adapterView.getItemAtPosition(i);
sharedPref.setCountry(selection);
}
});
materialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sharedPref.clear();
});

相关内容

  • 没有找到相关文章