0

I know this question has been asked and answered many times and I have tried all the methods suggested here and here but I am still not able to solve the problem so I am asking it again here.

I am downloading some images from web and displaying them in my recycler view. I have verified that I am getting the correct urls and images but when I am trying to update my recycler view I am getting the ViewRootImpl: sendUserActionEvent() mView == null error and my recycler view is not updating.

12-26 23:46:13.890 1509-1509/com.example.vuclip.gallaryapp D/ViewRootImpl: #3 mView = null

12-26 23:46:13.910 1509-1509/com.example.vuclip.gallaryapp E/ViewRootImpl: sendUserActionEvent() mView == null

I tried to debug this in 2 phones, Samsung S7 and Motorola G 2nd Gen and both gave me the same error.

I can add my code if you guys want but I don't think there is any bug in my code. Please help me with this, Thank you

UPDATE

//Main activity
public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();
private static final String apiURL = "http://api.androidhive.info/json/glide.json";
private RecyclerView recyclerView;
private ArrayList<Image> images;
private ProgressDialog pDialog;
private MyRecyclerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    pDialog = new ProgressDialog(this);
    images = new ArrayList<>();

    mAdapter = new MyRecyclerAdapter(images, getApplicationContext());
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

    fetchImages(apiURL);
}


private void fetchImages(String url) {
    new JSONDownloader().execute(url);
}

private void parseJson(String jsonString) {
    Log.d(TAG, "parseJson: json = " + jsonString);
    images.clear();
    JSONParser parser = new JSONParser(jsonString);
    images = parser.parse();
    mAdapter.notifyDataSetChanged();
}

private class JSONDownloader extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        pDialog.setMessage("Downloading JSON");
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
       String json = "";
        try {
            HttpHelper http = new HttpHelper(new URL(params[0]));
            json = http.getJSON();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Log.d(TAG, "doInBackground: json received = " + json);
        return json;
    }

    @Override
    protected void onPostExecute(String response) {
        if (pDialog.isShowing()) pDialog.cancel();

        parseJson(response);
    }
    }
}

Recycler adapter

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder>{
private static final String TAG = MyRecyclerAdapter.class.getSimpleName();
private List<Image> images;
private Context context;

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.gallery_thumbnail,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Image image = images.get(position);
    Log.d(TAG, "onBindViewHolder: image URL = " + image.getMedium_thumb_url());
    Glide.with(context)
            .load(image.getMedium_thumb_url())
            .thumbnail(0.5f)
            .crossFade()
            .into(holder.imageView);
}

@Override
public int getItemCount() {
    return images.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{

    private ImageView imageView;
    public MyViewHolder(View itemView) {
        super(itemView);
        imageView = (ImageView) itemView.findViewById(R.id.thumbnail);
    }
}

public MyRecyclerAdapter(List<Image> images, Context context) {
    this.images = images;
    this.context = context;
}
}
Community
  • 1
  • 1
Ezio
  • 2,837
  • 2
  • 29
  • 45

1 Answers1

1

You are creating new reference of your ArrayList which is different from the one which you set on Adapter of your RecyclerView.

Rather creating new reference use .addAll() property.

private void parseJson(String jsonString) {
   Log.d(TAG, "parseJson: json = " + jsonString);
   images.clear();
   JSONParser parser = new JSONParser(jsonString);
   // Use .addAll and avoid new refernce
   images.addAll(parser.parse());
   mAdapter.notifyDataSetChanged();
}
Rahul
  • 10,457
  • 4
  • 35
  • 55
  • Thanks a lot man, but I was not creating a new Object I was just assigning it to the arraylist that I returned from that function. Correct me if I'm wrong – Ezio Dec 26 '16 at 18:41
  • Actually new object was getting assign to your arrayList. So reference was getting changed – Rahul Dec 26 '16 at 18:41
  • I am little bit confused but I will try to figure it out by myself, Thank you for your help, now I can sleep peacefully :) – Ezio Dec 26 '16 at 18:43
  • You was not creating new object but inside of your parser.parse() method new ArrayList must be created, so your current ArrayList was holding its reference – Rahul Dec 26 '16 at 18:44