mkki
mkki

Reputation: 313

AsyncTask only works sometimes Android

I'm having a AsyncTask to return an array from an url. It works 80% of the time it executes, but the other 20% it crashes the app. When I click on the activity and click on the back button 10 times, the app starts to crash on the 5th time. Here is the relevant code and log cat.

    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)

Upvotes: 0

Views: 417

Answers (1)

M-Wajeeh
M-Wajeeh

Reputation: 17284

First of all the whole purpose of AsyncTask is destroyed here

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

you should just call new getFriendAsync().execute(); and move your remaining code in onPostExecute().

And secondly onCompleted() may get called when Activity is no longer active so you can get null pointer exception while trying to access GUI. So just add null check before accessing any UI element:

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

Upvotes: 1

Related Questions