使用RecyclerView出现Only the original thread that created a view hierarchy can touch its views异常
请看我的代码
packagecom.liqipeter.fruits;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.support.annotation.NonNull;
importandroid.support.design.widget.FloatingActionButton;
importandroid.support.design.widget.NavigationView;
importandroid.support.design.widget.Snackbar;
importandroid.support.v4.view.GravityCompat;
importandroid.support.v4.widget.DrawerLayout;
importandroid.support.v7.app.ActionBar;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.support.v7.widget.GridLayoutManager;
importandroid.support.v7.widget.LinearLayoutManager;
importandroid.support.v7.widget.RecyclerView;
importandroid.support.v7.widget.Toolbar;
importandroid.util.Log;
importandroid.view.Menu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.widget.Toast;
importcom.android.volley.VolleyError;
importcom.android.volley.toolbox.StringRequest;
importcom.android.volley.toolbox.Volley;
importcom.google.gson.Gson;
importorg.json.JSONException;
importjava.util.ArrayList;
importokhttp3.OkHttpClient;
importokhttp3.Request;
importokhttp3.Response;
public classMainActivityextendsAppCompatActivity {
privateFruitAdapterfruitAdapter;
privateDrawerLayoutmDrawerLayout;
public static final intNEWS_DATA=1;
privateRecyclerViewrecyclerView;
privateStringurl="http://v.juhe.cn/toutiao/index?type=top&key=";
privateHandlerhandler=newHandler(){
@Override
public voidhandleMessage(Message msg) {
switch(msg.what){
caseNEWS_DATA:
recyclerView.setAdapter(fruitAdapter);
break;
default:
}
}
};
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout= (DrawerLayout)findViewById(R.id.drawer_layout);
NavigationView navView=(NavigationView)findViewById(R.id.nav_view);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
GridLayoutManager layoutManager =newGridLayoutManager(this,2);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
fab.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
Snackbar.make(v,"Data deleted",Snackbar.LENGTH_SHORT).setAction(
"Undo", newView.OnClickListener() {
@Override
public voidonClick(View v) {
Toast.makeText(MainActivity.this,"sdf",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
ActionBar actionBar = getSupportActionBar();
if(actionBar !=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
}
navView.setCheckedItem(R.id.nav_call);
navView.setNavigationItemSelectedListener(newNavigationView.OnNavigationItemSelectedListener() {
@Override
public booleanonNavigationItemSelected(@NonNullMenuItem item) {
mDrawerLayout.closeDrawers();
return true;
}
});
getData();
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar,menu);
return true;
}
@Override
public booleanonOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
caseandroid.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
break;
caseR.id.backup:
Toast.makeText(this,"sdf",Toast.LENGTH_SHORT).show();
break;
caseR.id.delete:
Toast.makeText(this,"sdf",Toast.LENGTH_SHORT).show();
break;
caseR.id.settings:
Toast.makeText(this,"sdf",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
private voidgetData() {
newThread(newRunnable() {
@Override
public voidrun() {
try{
OkHttpClient client =newOkHttpClient();
Request request =newRequest.Builder().url(url).build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
showResponse(responseData);
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
private voidshowResponse(String responseData)throwsJSONException {
Gson gson =newGson();
Bean bean = gson.fromJson(responseData,Bean.class);
ArrayList dataBeen = (ArrayList) bean.getResult().getData();
fruitAdapter=newFruitAdapter(dataBeen);
GridLayoutManager layoutManager =newGridLayoutManager(this,2);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
Message message =newMessage();
message.what=NEWS_DATA;
handler.sendMessage(message);
}
}
之前我百度了,我以为是需要我开线程,所以我就用了Handler 结果发现还是存在这个异常。
然后我就把
GridLayoutManager layoutManager =newGridLayoutManager(this,2);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
这三句话提到了前面也就是我初始化控件的那里
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout= (DrawerLayout)findViewById(R.id.drawer_layout);
NavigationView navView=(NavigationView)findViewById(R.id.nav_view);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
GridLayoutManager layoutManager =newGridLayoutManager(this,2);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
。。。。。。。。。。。。后背省略。最后成功显示了。