Android 共享首选项在切换片段时导致空指针异常
我正在开发一个基于片段的Android应用程序。在这个应用程序中,它从网站上抓取一些信息,并将其存储在Android 共享首选项在切换片段时导致空指针异常,android,android-fragments,sharedpreferences,android-handler,android-timer,Android,Android Fragments,Sharedpreferences,Android Handler,Android Timer,我正在开发一个基于片段的Android应用程序。在这个应用程序中,它从网站上抓取一些信息,并将其存储在SharedReferences中,以比较用户想要显示的内容。因此,用户输入一些他们正在寻找的地图,应用程序每12秒就会抓取一次网页,如果它与用户的输入匹配,它就会发送一个通知。现在这在一个计时器上工作,它是一个处理程序,当你切换片段时,12秒后它停止并给出一个空指针异常 如果片段处于活动状态时工作正常,它为什么要这样做?下面是一些代码 关于创建方法 @Override public View
SharedReferences
中,以比较用户想要显示的内容。因此,用户输入一些他们正在寻找的地图,应用程序每12秒就会抓取一次网页,如果它与用户的输入匹配,它就会发送一个通知。现在这在一个计时器上工作,它是一个处理程序,当你切换片段时,12秒后它停止并给出一个空指针异常
如果片段处于活动状态时工作正常,它为什么要这样做?下面是一些代码
关于创建方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_map, container, false);
textView = (AutoCompleteTextView) v.findViewById(R.id.map_list);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, Maps1);
textView.setThreshold(3);
textView.setAdapter(adapter);
mButton1 = (Button) v.findViewById(R.id.MapButton1);
mButton2 = (Button) v.findViewById(R.id.MapButton2);
mButton3 = (Button) v.findViewById(R.id.MapButton3);
mButton1.setOnClickListener(onClick());
mButton2.setOnClickListener(onClick());
mButton3.setOnClickListener(onClick());
dButton1 = (Button) v.findViewById(R.id.DeleMap1);
dButton2 = (Button) v.findViewById(R.id.DeleMap2);
dButton3 = (Button) v.findViewById(R.id.DeleMap3);
dButton1.setOnClickListener(onClick());
dButton2.setOnClickListener(onClick());
dButton3.setOnClickListener(onClick());
MapTV1 = (TextView) v.findViewById(R.id.topText1);
MapTV2 = (TextView) v.findViewById(R.id.topText2);
MapTV3 = (TextView) v.findViewById(R.id.topText3);
LowTV1 = (TextView) v.findViewById(R.id.belowText1);
LowTV2 = (TextView) v.findViewById(R.id.belowText2);
LowTV3 = (TextView) v.findViewById(R.id.belowText3);
LowTV4 = (TextView) v.findViewById(R.id.testView);
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@SuppressWarnings("unchecked")
public void run() {
try {
//new Description().execute();
startTimer(120000);
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 120000);
//Notification area again *****************************************************************
userNotification = new NotificationCompat.Builder(getActivity());
userNotification.setAutoCancel(true);
// Done ***********************************************************************************
// Set user preferred maps on build *******************************************************
SharedPreferences spFirstWanted = getActivity().getSharedPreferences("first_map", 17);
String FirstWanted = spFirstWanted.getString("Firstwantedmap", "");
MapTV1.setText(FirstWanted);
SharedPreferences spSecondWanted = getActivity().getSharedPreferences("second_map", 18);
String SecondWanted = spSecondWanted.getString("Secondwantedmap", "");
MapTV2.setText(SecondWanted);
SharedPreferences spThirdWanted = getActivity().getSharedPreferences("third_map", 19);
String ThirdWanted = spThirdWanted.getString("Thirdwantedmap", "");
MapTV3.setText(ThirdWanted);
return v;
}
有些东西现在只是将测试内容打印到屏幕上,以确保SharedReferences
正常工作,谢谢您的帮助
错误日志
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:376)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:371)
at com.horizonservers.horizon.MapFragment$Medium$1.onFinish(MapFragment.java:658)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
删除片段后,
getActivity()
似乎返回null
因此,您应该首先存储getActivity()
或getActivity().getApplicationContext()
的值。(在可运行实例和介质实例中)
然后使用该值而不是
getActivity()
似乎getActivity()
在片段被删除后返回null
因此,您应该首先存储getActivity()
或getActivity().getApplicationContext()
的值。(在可运行实例和介质实例中)
然后使用该值而不是
getActivity()
,这是因为SharedReferences上下文在切换片段时被破坏,所以它现在指向一个以前被破坏的空对象
使用WeakReference或使用单例作为首选项实用程序
对于singleton首选项实用程序,您可以从中查看 这是因为SharedReferences上下文
在切换片段时被破坏,所以它现在指向一个以前被破坏的空对象
使用WeakReference或使用单例作为首选项实用程序
对于singleton首选项实用程序,您可以从中查看 发布错误日志发布错误日志发布错误日志发布错误日志发布错误日志我将如何做以及将其放置在何处?我将如何做以及将其放置在何处?我将在稍后查看easy首选项,看起来这可能是一个不错的选择。谢谢,我稍后会查看easy首选项,看起来这可能是一个不错的选择。非常感谢。
private void startTimer(long milliseconds) {
CountDownTimer counter = new CountDownTimer(milliseconds, 1200000){
public void onTick(long millisUntilDone) {
}
public void onFinish() {
new Description().execute(url);
new Map2().execute(url2);
new EasyTwo().execute(easyTwoURL);
new Medium().execute(mediumURL);
SharedPreferences spFirstWanted = PreferenceManager.getDefaultSharedPreferences(getActivity());
String FirstWanted = spFirstWanted.getString("Firstwantedmap", "");
SharedPreferences spSecondWanted = getActivity().getSharedPreferences("second_map", 18);
String SecondWanted = spSecondWanted.getString("Secondwantedmap", "");
SharedPreferences spThirdWanted = getActivity().getSharedPreferences("third_map", 19);
String ThirdWanted = spThirdWanted.getString("Thirdwantedmap", "");
MapTV3.setText(FirstWanted);
SharedPreferences beginnerSP = getActivity().getSharedPreferences("beginner", 20);
String BeginnerValue = beginnerSP.getString("BeginnerMap", "");
SharedPreferences easySP = getActivity().getSharedPreferences("easy", 21);
String EasyValue = easySP.getString("EasyMap", "");
SharedPreferences easyTwoSP = getActivity().getSharedPreferences("easyTwo", 22);
String EasyTwoValue = easyTwoSP.getString("EasyTwoMap", "");
SharedPreferences mediumSP = getActivity().getSharedPreferences("medium", 23);
String mediumValue = mediumSP.getString("MediumMap", "");
//Toast.makeText(getActivity(),/*"beginner " + prefs2 + " and easy " + prefs3 + " and easy2 " + prefs4 +*/ " medium " + mediumValue,Toast.LENGTH_LONG).show();
if (BeginnerValue.equals(FirstWanted)) {
setUserNotification(BeginnerValue, "Beginner");
} else if(EasyValue.equals(FirstWanted)) {
setUserNotification(EasyValue, "Easy");
} else if(EasyTwoValue.equals(FirstWanted)) {
setUserNotification(EasyTwoValue, "Easy Two");
} else if(mediumValue.equals(FirstWanted)) {
setUserNotification(mediumValue, "Medium");
} else {
System.out.print("They Don't Match!");
LowTV3.setText("They don't match!");
}
}
};
counter.start();
}
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:376)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:371)
at com.horizonservers.horizon.MapFragment$Medium$1.onFinish(MapFragment.java:658)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)