0

Im getting this error randomly during on click, it doesn't happen every time just occasionally.

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.

import android.content.*;
import android.os.*;
import android.support.v4.app.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;


import java.util.*;
import org.json.*;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.theappguru.fixturesync.Settings;

import static com.google.android.gms.internal.zzahg.runOnUiThread;


public class menufragment extends Fragment {


    private String TAG = MainActivity.class.getSimpleName();
    public ListView list;
    public ArrayList<menufragmentinfos> countries = new ArrayList<menufragmentinfos>();
    public menufragmentadapter adapter;
    private MainActivity mActivity;
    private FirebaseAnalytics mFirebaseAnalytics;

    public menufragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);





    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(com.theappguru.fixturesync.R.layout.menufragment, container, false);
    }

    private class Wrapper
    {

        String Title = "Undefinded";

    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        LinearLayout loading = (LinearLayout) getView().findViewById(com.theappguru.fixturesync.R.id.loading);
        loading.setVisibility(View.VISIBLE);

        mFirebaseAnalytics = FirebaseAnalytics.getInstance(getActivity());

        list = (ListView) getView().findViewById(com.theappguru.fixturesync.R.id.list);
        adapter = new menufragmentadapter(this);

        list.setAdapter(adapter);


        list.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                String type = countries.get(position).type;
                String data = countries.get(position).data;
                String link = countries.get(position).link;
                String availability = countries.get(position).availability;
                String date = countries.get(position).date;
                String name = countries.get(position).name;




                if (type.equals("fixture")){
                    mActivity = (MainActivity) getActivity();

                    Bundle bundle = new Bundle();
                    bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
                    bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, type);
                    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

                    String url = link;
                    String username = "";
                    String password = "";
                    Settings settings = mActivity.getSettings();
                    settings.putString(Settings.PREF_LASTURL, url);

                    SharedPreferences msharedpref = getActivity().getSharedPreferences("calendar" , 0);
                    SharedPreferences.Editor editor = msharedpref.edit();
                    editor.putString("link", url);
                    editor.putString("name", name);
                    editor.putString("type", "");
                    editor.apply();


                    mActivity.setSource(url, null, username, password);
                    mActivity.test();}


                if (type.equals("menu")){

                    if (availability.equals("not available")){
                        Toast.makeText(getActivity().getApplicationContext(),
                                "Fixtures Not Published Yet Please Check Back After "+date,
                                Toast.LENGTH_LONG)
                                .show();
                    }
                    else {

                        try {


                            Bundle bundle1 = new Bundle();
                            bundle1.putString("message", countries.get(position).data);
                            menufragment fragInfo = new menufragment();
                            fragInfo.setArguments(bundle1);

                            FragmentManager fm = getFragmentManager();
                            FragmentTransaction fragmentTransaction = fm.beginTransaction();
                            fragmentTransaction.add(com.theappguru.fixturesync.R.id.changeFragment, fragInfo);
                            fragmentTransaction.addToBackStack("tag");
                            fragmentTransaction.commit();


                        }
                        catch (Exception e) {
                            Log.e("menu", e.toString());
                        }

                    }
                }

                // Making a request to url and getting response




            }
        });










        new GetContacts().execute();




        super.onViewCreated(view, savedInstanceState);
    }




    private class GetContacts extends AsyncTask<Void, Void, Wrapper> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();


        }

        @Override
        protected Wrapper doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            Wrapper w = new Wrapper();
            String Title = "a";




            String jsonStr = getArguments().getString("message");





            Log.e(TAG, "Response from url: " + jsonStr);



            if (jsonStr != null) {
                try {





                    countries.clear();

                    JSONObject data_array=new JSONObject(jsonStr);

                    JSONArray phone = data_array.getJSONArray("data");








                    for (int i = 0 ; i < phone.length() ; i++) {
                        JSONObject obj = new JSONObject(phone.get(i).toString());

                        menufragmentinfos add = new menufragmentinfos();
                        add.name = obj.getString("name");

                        String type = obj.getString("type");
                        String availability = obj.optString("availability");
                        String date = obj.optString("date");


                        add.type = type;
                        add.availability = availability;
                        add.date = date;
                        if (type.equals("menu")){
                            add.data = (obj.toString());
                        }
                        if (type.equals("fixture")){
                            add.link = obj.getString("link");
                        }









                        countries.add(add);


                    }



                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.toString());



                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");


            }

            return w;
        }


        @Override
        protected void onPostExecute(Wrapper w) {
            super.onPostExecute(w);

            runOnUiThread(new Runnable()
            {
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }});



            LinearLayout loading = (LinearLayout) getView().findViewById(com.theappguru.fixturesync.R.id.loading);
            loading.setVisibility(View.GONE);


        }


    }







    @Override
    public void onPause() {
        runOnUiThread(new Runnable()
        {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }});
        super.onPause();

    }

    /** Called when returning to the activity */
    @Override
    public void onResume() {

        runOnUiThread(new Runnable()
        {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }});
        super.onResume();



    }

    /** Called before the activity is destroyed */
    @Override
    public void onDestroy() {

        super.onDestroy();


    }





}

Adapter

import android.content.*;
import android.view.*;
import android.widget.*;

public class menufragmentadapter extends BaseAdapter {

    private RelativeLayout background;

    menufragment main;

    menufragmentadapter(menufragment main)
    {
        this.main = main;
    }

    @Override
    public int getCount() {
        return  main.countries.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolderItem {
        TextView name;


    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolderItem holder = new ViewHolderItem();
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) main.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(com.theappguru.fixturesync.R.layout.menufragmentcells, null);

            holder.name = (TextView) convertView.findViewById(com.theappguru.fixturesync.R.id.name);


            convertView.setTag(holder);
        }
        else
        {




            holder = (ViewHolderItem) convertView.getTag();
        }


        holder.name.setText(this.main.countries.get(position).name);














        return convertView;


    }



}

log

15 14:22:11.977 16877-16877/com.theappguru.fixturesync E/AndroidRuntime: FATAL EXCEPTION: main Process: com.theappguru.fixturesync, PID: 16877 java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131361839, class android.widget.ListView) with Adapter(class com.theappguru.fixturesync.ui.menufragmentadapter)] at android.widget.ListView.layoutChildren(ListView.java:1623) at android.widget.AbsListView.onLayout(AbsListView.java:2205) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:725) at android.view.View.layout(View.java:17564) at android.view.ViewGroup.layout(ViewGroup.java:5654) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2406) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2126) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6536) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871) at android.view.Choreographer.doCallbacks(Choreographer.java:683) at android.view.Choreographer.doFrame(Choreographer.java:619) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6247) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
alex barnes
  • 33
  • 1
  • 6
  • Possible duplicate of [Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"](https://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed) – Dima Kozhevin Oct 15 '17 at 13:33

1 Answers1

-1

Your doInBackground method is modifying the contents of the adapter (clearing the list and populating it again) and at the same time your adapter is also referencing the same variable. Use a different reference for the countries list inside of the adapter and populate it onPostExecute method.

Amit Barjatya
  • 249
  • 3
  • 14
  • Ok thanks ill try that, i thought as the do in background is only called once on create and not again this wouldnt be a problem. – alex barnes Oct 15 '17 at 15:16
  • May i know, why the downvote? did you try it this way and didnt work? – Amit Barjatya Oct 15 '17 at 16:01
  • The downvote wasnt from me. Ive just tried it and unfortunately it didnt solve the problem. Thanks anyway though. – alex barnes Oct 15 '17 at 16:05
  • ohh okay. If i get time, i'll try your code and see if i can make it work. did you try going through the link which Dima Kozhevin commented on your question ? – Amit Barjatya Oct 15 '17 at 18:28