使用Retrofit在OnResume()中获取NullPointerException

时间:2015-04-21 14:26:52

标签: java android android-fragments callback retrofit

我们正在尝试从服务器读取JSON文件,并使用Retrofit GET功能将其显示在我们的Android应用上。 我们将代码基于现有的Android应用程序,但我们使用的是Activity(lib:android.support.v4.app.Fragment),而不是Fragment。运行此操作会给我们带来以下错误

Caused by: java.lang.NullPointerException
        at com.example.myapp2.AgendaSettingModule.onResume(AgendaSettingModule.java:50)

这是我们的类调用Retrofit方法

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

和我们的界面有Retrofit 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
    }
}

1 个答案:

答案 0 :(得分:0)

我建议从DemoApplicaiton

中输入
 questionASMService = createQuestionASMService(this);

在onResume内而不是onCreate

public class DemoApplication extends Application {
    private static QuestionASMService questionASMService;

    @Override
    public void onCreate() {
        super.onCreate();
        questionASMService = createQuestionASMService(this);
    }

替换为

public class DemoApplication extends Application {
    private static QuestionASMService questionASMService;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onResume() {
        super.onResume();
        questionASMService = createQuestionASMService(this);
    }