Recycler视图没有更新和日志说ViewRootImpl:sendUserActionEvent()mView == null

时间:2016-12-26 18:24:02

标签: android android-recyclerview

我知道这个问题已经被多次询问和回答了,我已经尝试了所有建议的方法herehere但是我仍然无法解决问题,所以我在这里再次提出这个问题。

我正在从网上下载一些图像并在我的回收站视图中显示它们。我已经验证我正在获取正确的网址和图片但是当我尝试更新我的回收站视图时,我收到 ViewRootImpl:sendUserActionEvent()mView == null 错误,我的回收站视图未更新

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

我尝试在2部手机中调试这个,三星S7和摩托罗拉G第二代都给了我同样的错误。

如果你们想要我可以添加我的代码,但我不认为我的代码中有任何错误。 请帮帮我,谢谢

更新

//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;
}
}

1 个答案:

答案 0 :(得分:1)

您正在创建ArrayList的新引用,该引用与您在Adapter的{​​{1}}上设置的引用不同。

而是创建新的引用使用.addAll()属性。

RecyclerView