2014-10-10 57 views
-1

我有一個適用於我的應用程序的導航抽屜,其中包含一個可以在片段之間導航的菜單。我在應用程序午餐後得到的第一個片段是Home,它包含一個listView和一個附在底部的頁腳(僅用於測試)。問題是,當我第一次吃Home片段時沒有發生任何事情並且它的工作非常好,但是當我從導航抽屜中更改片段並嘗試返回Home片段時,應用程序崩潰。錯誤日誌顯示一個空指針異常。帖是我的代碼細節onPostExecute方法上的NullPointerException

onCreateView方法

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (v != null) { 
      ViewGroup parent = (ViewGroup) v.getParent(); 
      if (parent != null) 
       parent.removeView(v); 
     } 
      try { 


       v = inflater.inflate(R.layout.fragment_homes, container, false); 

       listView = (ListView) v.findViewById(R.id.homeList); 

       l = new HomeListAdapter(this, homeList); 
       if(l == null) 
        Log.i("tag","null"); 

       listView.setAdapter(l); 
      } catch (InflateException e) { 
     } 

     return v; 

    } 

onPostExecute方法

@Override 
     protected void onPostExecute(final String res) { 

      /*try { 
       jObj = new JSONArray(res); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      }*/ 

      /* JSONArray j = null; 
      try { 
       j = new JSONArray(res); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      }*/ 


      // Parsing json 
      for (int i = 0; i < 5; i++) { 
       //JSONObject obj = j.getJSONObject(i); 

       Home home = new Home(); 
       //String photoNews = java.net.URLDecoder.decode(obj.getString("photonews"), "UTF-8"); 
       home.setNomC("Karim Ennassiri"); 
       home.setPhotoP("http://i1239.photobucket.com/albums/ff509/FestusMANAFEST/iker-casillas20Wallpaper__yvt2.jpg"); 
       home.setPhotoAr("http://voyage-essaouira-maroc.com/upload/apostrophe_image_(13).jpg"); 
       home.setText("Chlada V2.0 Edition"); 
       home.setNbrSick("5"); 
       home.setNbrComm("8"); 
       home.setNbrShare("0"); 

       homeList.add(home); 

      } 

      l.notifyDataSetChanged(); 

      //pDialog.hide(); 


     } 

錯誤日誌

10-10 09:31:30.884 2447-2447/com.example.user.unchained E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.example.user.unchained, PID: 2447 
java.lang.NullPointerException 
     at com.example.user.unchained.HomesActivity$PlaceholderFragment$FeedsTask.onPostExecute(HomesActivity.java:375) 
     at com.example.user.unchained.HomesActivity$PlaceholderFragment$FeedsTask.onPostExecute(HomesActivity.java:277) 
     at android.os.AsyncTask.finish(AsyncTask.java:632) 
     at android.os.AsyncTask.access$600(AsyncTask.java:177) 
     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5017) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
     at dalvik.system.NativeStart.main(Native Method) 

而這正是導致錯誤的行:l.notifyDataSetChanged();。我注意到,錯誤只是在我試圖顯示片段時間的情況下。

那麼請幫忙嗎?謝謝 !

+0

將此代碼移動到'onPostExecute()'方法中。 'l = new HomeListAdapter(this,homeList); (l == NULL) Log.i(「tag」,「null」); listView.setAdapter(l);'因爲你的UI在那個方法中被更新。 – Piyush 2014-10-10 10:05:06

+0

但是,如果我會這樣做'this'將引用任務而不是片段 – Karimx 2014-10-10 10:10:36

+0

所以你需要使用'getActivity()'而不是'this'。 – Piyush 2014-10-10 10:11:17

回答

0

你需要在doInBackground中使用它。

   @Override 
    protected Void doInBackground(void...) { 
     try { 

      for (int i = 0; i < 5; i++) { 
      //JSONObject obj = j.getJSONObject(i); 

      Home home = new Home(); 
      //String photoNews = java.net.URLDecoder.decode(obj.getString("photonews"), "UTF-8"); 
      home.setNomC("Karim Ennassiri"); 
      home.setPhotoP("http://i1239.photobucket.com/albums/ff509/FestusMANAFEST/iker-casillas20Wallpaper__yvt2.jpg"); 
      home.setPhotoAr("http://voyage-essaouira-maroc.com/upload/apostrophe_image_(13).jpg"); 
      home.setText("Chlada V2.0 Edition"); //give some other name in getter setter for setText in Home.java. It makes not to understand whether you are using setText for textview assigning. 
      home.setNbrSick("5"); 
      home.setNbrComm("8"); 
      home.setNbrShare("0"); 

      homeList.add(home); 

     } 

postExecute用於顯示UI。不要在PostExecute中執行像解析這樣的繁重操作。

+0

還有問題 – Karimx 2014-10-10 10:28:01

相關問題