Sorry for asking near same questions, cause my low reputation can't comment there.

So I am using parse.com and doing a query for users and putting them in a list that in fragment.

The app works, but when I switching between fragments fast, it crashes with exception: java.lang.IllegalStateException: Content view not yet created

before my code was in onResume, but as I understand need to be in onCreateView, still don't understand why, cause onResume comes after onCreateView. Anyway this is mu code:

public class FriendsFragment extends ListFragment {

protected List<ParseUser> mFriends;
protected ParseUser mCurrentUser;
protected ParseRelation<ParseUser> mFriendsRelation;

@InjectView(R.id.fragmentFriendsSpinner) ProgressBar mSpinner;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_friends, container, false);
    ButterKnife.inject(this, rootView);
    mSpinner.setVisibility(View.INVISIBLE);

    mCurrentUser = ParseUser.getCurrentUser();
    mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);

    ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
    query.orderByAscending(ParseConstants.KEY_LASTNAME);
    query.addAscendingOrder(ParseConstants.KEY_FIRSTNAME);
    mSpinner.setVisibility(View.VISIBLE);
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> friends, ParseException e) {
            mSpinner.setVisibility(View.INVISIBLE);
            if (e == null) {
                // Success
                mFriends = friends;
                int usersAmount = mFriends.size();
                String[] fullNames = new String[usersAmount];
                int i = 0;
                for (ParseUser user : mFriends) {
                    fullNames[i] = user.getString(ParseConstants.KEY_FIRSTNAME) + " " +
                            user.getString(ParseConstants.KEY_LASTNAME);
                    i++;
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        getListView().getContext(),
                        android.R.layout.simple_list_item_1,
                        fullNames
                );
                setListAdapter(adapter);
            } else {
                mSpinner.setVisibility(View.INVISIBLE);
                // Show error to user
                OkCustomDialog dialog = new OkCustomDialog(
                        getListView().getContext(),
                        getString(R.string.friend_list_updating_error_title),
                        e.getMessage());
                dialog.show();

            }
        }
    });

    return rootView;
}
}

and this catLog I received:

06-11 08:18:47.526  25134-25134/com.example.android.donotbelateapp     W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception  (group=0x41c54c08)
06-11 08:18:47.531  25134-25134/com.example.android.donotbelateapp  E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.donotbelateapp, PID: 25134
java.lang.IllegalStateException: Content view not yet created
        at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
        at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
        at com.example.android.donotbelateapp.ui.fragments.FriendsFragment$1.done(FriendsFragment.java:71)
        at com.example.android.donotbelateapp.ui.fragments.FriendsFragment$1.done(FriendsFragment.java:56)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        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:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)

line 71 is ArrayAdapter adapter = new ArrayAdapter(

Thanks for any help.

1

There are 1 answers

0
QAMAR On BEST ANSWER

This is because public void done(List friends, ParseException e) is executed when response of background task is received BUT your fragment is not present there as its destroyed. So the app crashes.

I think you should put the loaded data in storage(DB), OR ApplicationData(Not Recomended). so In your fragment check if data is already available then do not load it.