0

Я работаю над флеш-файловым Android-приложением. В этом приложении он сбрасывает некоторую информацию с веб-сайтов и сохраняет ее в SharedPreferences, чтобы сравнить то, что пользователь хочет отобразить. Таким образом, пользователь вводит какую-то карту, которую они ищут, приложение разбивает веб-страницы каждые 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; 
} 

Один из Asynctasks, что подбрасывает исключения нулевого указателя

class Medium extends AsyncTask<String, String, String> { 
    String desc; 

    protected String doInBackground(String... params) { 
    try { 
     Document document = Jsoup.connect(mediumURL).get(); 
     Element table = document.select("table").get(0); //select the first table. 
     Elements rows = table.select("tr:eq(6) > td.infodata > a "); 

     for (Element td : rows) { 
     System.out.println("TD" + td.text()); 
     return (td.text()); 
     } 
     //return (td.text()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
    } 

    protected void onPostExecute(final String result3) { 
    if (!result3.equals(null)) { 
     new CountDownTimer(6000, 1000) { 
     public void onTick(long millisUntilFinished) { 
     } 

     public final void onFinish() { 
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
      //SharedPreferences sp = getActivity().getSharedPreferences("medium", 23); 
      SharedPreferences.Editor sedt = sp.edit(); 
      sedt.putString("MediumMap", result3); 
      sedt.commit(); 

     } 
     }.start(); 
     LowTV4.setText(result3); 

    } else if (result3.equals(null)) { 
     MapTV3.setText("Choose a map"); 
    } else if (result3.equals("")) { 
     MapTV3.setText("Choose a new map"); 
    } 
    } 
} 

таймера

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

Некоторые из материалов просто печатают тестовые материалы на экране, чтобы убедиться, что SharedPreferences работает, спасибо за вашу помощь.

Журнал ошибок

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) 
+0

опубликовать журнал ошибок –

+0

опубликовал журнал ошибок – Destry

ответ

1

Это потому, что при переключении фрагмента был уничтожен SharedPreferences context, поэтому теперь он указывает на нулевой объект, который был уничтожен раньше.

Либо использовать WeakReference или использовать Singleton для полезности предпочтения.

Для однопользовательской утилиты, вы можете посмотреть Prefs.java от Pixplicity EasyPreferences.

+0

Я буду смотреть на удобные настройки позже, похоже, что это может быть хорошим вариантом. Спасибо – Destry

+0

Добро пожаловать;) –

1

Кажется, что getActivity() возвращение null после фрагмента удаляется.

Таким образом, вы должны запасти значение getActivity() или getActivity().getApplicationContext(). (Как в экземпляре Runnable, так и в экземпляре Medium)

Затем используйте значение вместо getActivity().

+0

Как бы я это сделал и где бы я его разместил? – Destry

Смежные вопросы