你好我的自定義類中有getter問題,我不知道爲什麼每次都返回null。當服務器響應沒有任何錯誤時,我正在設置值。在調試時,我發現服務器的響應正常,並且創建了對象的新實例,但是當我嘗試在我的活動中獲取它時,會出現空值。這裏有幾行代碼在哪裏是一個問題(在我看來)。從我的自定義類使用getter時出現NullPointerException
方法:
public void responseFromServer(){
showProgressDialog();
Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
TitleInterface titleInterface = retrofit.create(TitleInterface.class);
Call<MovieResponse> call = titleInterface.getMovie(API_KEY,movie);
call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
List<Movie> movieList = response.body().getMovieList();
ItemAdapter itemAdapter = new ItemAdapter(context.getApplicationContext(),generateData(movieList));
setItemAdapter(itemAdapter);
}
@Override
public void onFailure(Call<MovieResponse> call, Throwable t) {
Toast.makeText(context, "ERROR", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
});
}
,這裏是我的活動:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_list);
movieListView = (ListView) findViewById(R.id.movieListView);
String movie = getIntent().getStringExtra(TAG);
presenter = new Presenter(this,movie);
presenter.responseFromServer();
item=presenter.getItemAdapter();
movieListView.setAdapter(presenter.getItemAdapter());
presenter.getItemAdapter().notifyDataSetChanged();
presenter.getProgressDialog().dismiss();
}
感謝您的幫助。
你的通話是異步的。該值在您嘗試獲取之前未設置。 – njzk2
如何解決這個問題? – FinalDestination
這是什麼自定義類?服務? – Jaythaking