-1
以下是我主要活動的代碼。我正在使用改造創建項目,但在運行時強行關閉
我使用Retrofit從電影數據庫API調用數據,但是當我運行它時,它已安裝但強制關閉。我想知道錯誤的哪個部分,這是我主要活動的代碼。
public class MainActivity extends AppCompatActivity {
CustomAdapter adapter;
RecyclerView mrecyclerview;
List<Result> resultList = new ArrayList<>();
RecyclerView.LayoutManager layoutmanager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mrecyclerview= (RecyclerView) findViewById(R.id.recyclerview);
layoutmanager = new LinearLayoutManager(this);
adapter = new CustomAdapter(resultList);
mrecyclerview.setAdapter(adapter);
mrecyclerview.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
new RequestMovie().execute("popular");
}
@Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()== R.id.action_popular){
new RequestMovie().execute("popular");
} else if (item.getItemId()==R.id.action_top_rated){
new RequestMovie().execute("top_rated");
}
return super.onOptionsItemSelected(item);
}
private class RequestMovie extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... params) {
String kategori = params[0];
if (kategori.equals("popular")){
ApiInterface apiInterface = ApiClient.getRetrofit()
.create(ApiInterface.class);
Call <EldarMovie> call = apiInterface.getPopular();
call.enqueue(new Callback<EldarMovie>() {
@Override
public void onResponse(Call<EldarMovie> call, Response<EldarMovie> response) {
EldarMovie movie=response.body();
adapter.setData(movie.getResults());
}
@Override
public void onFailure(Call<EldarMovie> call, Throwable t) {
}
});
}
return null;
}
}
}
從我的應用程序強制關閉的代碼有什麼不對嗎?
首先,你爲什麼使用asyncTask,改造使用線程本身,你不需要asynctask –
請添加你的崩潰日誌 –
添加logcat報告,爲什麼你使用asyncTask –