2016-12-26 50 views
0

我知道這個問題已被問及和回答了很多次,我已經嘗試了所有的方法建議herehere但我仍然無法解決問題,所以我在這裏再次問。Recycler視圖不更新和日誌說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第二代調試這兩個都給了我同樣的錯誤。

我可以添加我的代碼,如果你們想,但我不認爲我的代碼中有任何錯誤。 請幫助我,謝謝

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

回收站適配器

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; 
} 
} 
+0

請分享您的代碼! – Eenvincible

+0

@Eenvincible我添加了我的MainActivity和Recycler Adapter。還有其他文件解析和下載JSON,但我不認爲有任何問題 – Ezio

+0

你可以嘗試清理你的項目和重建? – Eenvincible

回答

1

您創建ArrayList的新的參考是從一個不同的你設置爲RecyclerViewAdapter

而是創建新的引用使用.addAll()屬性。

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(); 
} 
+0

非常感謝,但我沒有創建一個新對象我只是將它分配給我從該函數返回的ArrayList。糾正我,如果我錯了 – Ezio

+0

實際上新的對象被分配給你的arrayList。所以參考文獻已經變了 – Rahul

+0

我有點困惑,但我會試着自己弄清楚,謝謝你的幫助,現在我可以安然入睡:) – Ezio