2013-09-29 80 views
0

我有一個AsyncTask從一個url返回一個數組。它在80%的執行時間內工作,但另外20%會使應用程序崩潰。當我點擊該活動並點擊後退按鈕10次時,該應用程序在第5次開始崩潰。這裏是相關的代碼和日誌貓。AsyncTask只適用於Android

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.friend_picker); 

     ActionBar mainBar = getActionBar(); 
     mainBar.hide(); 

     arraylist = new ArrayList<FacebookFriend>(); 

     OpenSession(); 

     checkButtonClick(); 
     pickAllButton(); 
    } 

public void OpenSession() { 
     Session.openActiveSession(this, true, new Session.StatusCallback() { 

      // Calls whenever it changes state. 
      @SuppressWarnings("deprecation") 
      @Override 
      public void call(Session session, SessionState state, 
        Exception exception) { 
       if (session.isOpened()) { 
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() { 
         @Override 
         public void onCompleted(GraphUser user, Response response) 
         { 
          getOwnId = user.getId(); 
         } 
        }); 

        Request.executeMyFriendsRequestAsync(session, new Request.GraphUserListCallback() { 

         @Override 
         public void onCompleted(List<GraphUser> users, Response response) { 

           ArrayList<String> checkedFriend = null; 
           try { 
            checkedFriend = new getFriendAsync().execute().get(); 
           } catch (InterruptedException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } catch (ExecutionException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } 

          if (users != null) { 
           for (GraphUser user : users) { 
            String userURL = "http://graph.facebook.com/"+user.getId().toString()+"/picture?width=100&height=100"; 

            fbFriend = new FacebookFriend(user.getId().toString(), user.getName().toString(), userURL, false); 

            if(checkedFriend.contains(user.getId().toString())) 
             fbFriend.setSelected(true); 

            arraylist.add(fbFriend); 
           } 
           Collections.sort(arraylist); 

           listview = (ListView) findViewById(R.id.mainListView); 
           adapter = new ListViewAdapterFb(FriendPicker.this,R.layout.simplerow,arraylist); 
           listview.setAdapter(adapter); 
          } 
         } 
        }); 
       } 
      } 
     }); 

private class getFriendAsync extends AsyncTask<Void, Void, ArrayList<String>> { 

     @Override 
     protected void onPreExecute() { 
     } 

     @Override 
     protected ArrayList<String> doInBackground(Void... params) { 

      String jsonobject; 
      JSONArray jsonA; 
      ArrayList<String> list = null; 
      try { 
       jsonobject = JSONfunctions.getJSONfromURL("http://tikipass.com/get_followed.php?id=" + getOwnId); 
       jsonA = new JSONArray(jsonobject); 
       list = new ArrayList<String>(); 
       for(int i = 0; i < jsonA.length(); i++) { 
        list.add(jsonA.getString(i)); 
       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return list; 
     } 

     protected void onPostExecute(ArrayList<String> args) { 
     } 
    } 

LOG CAT

09-28 22:44:14.474: E/AndroidRuntime(14289): FATAL EXCEPTION: main 
09-28 22:44:14.474: E/AndroidRuntime(14289): java.lang.NullPointerException 
09-28 22:44:14.474: E/AndroidRuntime(14289): at com.tikipass.FriendPicker$1$2.onCompleted(FriendPicker.java:107) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at com.facebook.Request$2.onCompleted(Request.java:289) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at com.facebook.Request$4.run(Request.java:1634) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at android.os.Handler.handleCallback(Handler.java:730) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at android.os.Handler.dispatchMessage(Handler.java:92) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at android.os.Looper.loop(Looper.java:137) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at android.app.ActivityThread.main(ActivityThread.java:5103) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at java.lang.reflect.Method.invokeNative(Native Method) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at java.lang.reflect.Method.invoke(Method.java:525) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
09-28 22:44:14.474: E/AndroidRuntime(14289): at dalvik.system.NativeStart.main(Native Method) 

回答

1

AsyncTask所有的全部目的,第一個是在這裏

new getFriendAsync().execute().get(); 

摧毀你應該叫new getFriendAsync().execute();onPostExecute()移動你剩下的代碼。

其次,當Activity不再活動時,onCompleted()可能會被調用,因此您可以在嘗試訪問GUI時獲得空指針異常。 因此,只需在訪問任何UI元素之前添加空值檢查:

listview = (ListView) findViewById(R.id.mainListView); 
if(listview != null){  
    adapter = new ListViewAdapterFb(FriendPicker.this,R.layout.simplerow,arraylist); 
    listview.setAdapter(adapter); 
}