0

我有一个 AsyncTask 从一个 url 返回一个数组。它在执行的 80% 的时间里工作,但另外 20% 的时间让应用程序崩溃。当我单击活动并单击后退按钮 10 次时,该应用程序在第 5 次开始崩溃。这是相关的代码和日志猫。

    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) {
        }
    }

日志猫

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)
4

1 回答 1

1

首先,这里的全部目的都AsyncTask被破坏了

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

您应该只调用new getFriendAsync().execute();并将剩余的代码移入onPostExecute().

其次,当不再活动onCompleted()时可能会被调用,因此您可以在尝试访问 GUI 时获得空指针异常。Activity因此,只需在访问任何 UI 元素之前添加空检查:

listview = (ListView) findViewById(R.id.mainListView);
if(listview != null){    
    adapter = new ListViewAdapterFb(FriendPicker.this,R.layout.simplerow,arraylist);
    listview.setAdapter(adapter);  
}
于 2013-09-29T06:24:57.683 回答