0
我們試圖從服務器讀取JSON
文件,並使用Retrofit GET函數將其顯示在我們的Android應用程序中。 我們將我們的代碼基於現有的和正在運行的Android應用程序,但不是Activity
,而是使用Fragment
(lib:android.support.v4.app.Fragment)。運行這給了我們以下錯誤使用Retrofit在OnResume()中獲取NullPointerException
Caused by: java.lang.NullPointerException
at com.example.myapp2.AgendaSettingModule.onResume(AgendaSettingModule.java:50)
這是我們班調用方法改造
import adapter.QuestionASMAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import application.DemoApplication;
import model.QuestionASM;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import java.util.List;
public class AgendaSettingModule extends Fragment implements Callback<List<QuestionASM>>{
private QuestionASMAdapter questionASMAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.agendasettings_module, container, false);
questionASMAdapter=new QuestionASMAdapter(view.getContext());
ListView listView=(ListView) view.findViewById(R.id.listQuestionASM);
View emptyView=view.findViewById(R.id.txtEmptyASM);
listView.setAdapter(questionASMAdapter);
listView.setEmptyView(emptyView);
return view;
}
@Override
public void onResume() {
super.onResume();
DemoApplication.getQuestionASMService().getQuestionsASM(this);
//This is where the NullPointerException occurs (line 50)
}
@Override
public void success(List<QuestionASM> questionASMs, Response response) {
questionASMAdapter.setQuestionsAsm(questionASMs);
}
@Override
public void failure(RetrofitError retrofitError) {
Toast.makeText(getView().getContext(), getString(R.string.error), Toast.LENGTH_LONG).show();
}
}
DemoApplication.java
public class DemoApplication extends Application {
private static QuestionASMService questionASMService;
@Override
public void onCreate() {
super.onCreate();
questionASMService = createQuestionASMService(this);
}
public static QuestionASMService getQuestionASMService() {
return questionASMService;
}
private QuestionASMService createQuestionASMService(Context context) {
final Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
return new RestAdapter.Builder()
.setConverter(new GsonConverter(gson))
.setEndpoint(context.getString(R.string.baseUrl))
.setLogLevel(RestAdapter.LogLevel.FULL)
.build()
.create(QuestionASMService.class);
}
}
和我們的界面具有改造GET
import retrofit.Callback;
import retrofit.http.Body;
import retrofit.http.GET;
import java.util.List;
public interface QuestionASMService {
@GET("/questions.html")
void getQuestionsASM(Callback<List<QuestionASM>> callback);
}
而這正是我們基於
public class QuestionsActivity extends Activity implements Callback<List<Question>> {
private QuestionAdapter questionAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
@Override
protected void onResume() {
super.onResume();
getDemoService().getQuestions(this);
}
@Override
public void success(final List<Question> questions, final Response response) {
questionAdapter.setQuestions(questions);
}
@Override
public void failure(final RetrofitError error) {
Toast.makeText(this, getString(R.string.tFailure), Toast.LENGTH_LONG).show(); //Foutmelding
Log.e(TAG, error.toString()); //Logging
}
}
I不太明白,我們使用DemoApplication中的getQuestionASMService()來返回該對象。 – Edward
我已經更新了我的答案,以便更加明確 – Vyncent
這是不可能的,因爲DemoApplication擴展了應用程序,並且沒有這樣的方法被覆蓋 http://developer.android.com/reference/android/app/Application.html – Edward