【发布时间】:2018-10-14 18:23:29
【问题描述】:
我正在尝试使用此 api 联网: https://api.jikan.moe/v3/schedule
所以我通过创建 ApiClient.java 类来使用改造,这是它的代码:
public class ApiClient {
public static final String BASE_URL = "http://api.themoviedb.org/3/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
链接的完整端点接口是这样的:
public interface ApiInterface {
@GET("schedule")
Call<MainResponse> getSchedule();
}
所以我在建模数据中使用了可序列化,并创建了 MainResponse.java 类来获取 api 中的主要星期一数组:
公共类 MainResponse {
@SerializedName("monday")
private List<Schedule> monday;
public List<Schedule> getMonday() {
return monday;
}
public void setMonday(List<Schedule> monday) {
this.monday = monday;
}
}
然后我创建一个新的建模类来获取类 Schedule.java 中星期一数组的对象项列表:
public class Schedule {
@SerializedName("title")
private String title;
public Schedule(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
最后在 MainActivity 我称之为:
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.schedule_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<MainResponse> call = apiService.getSchedule();
call.enqueue(new Callback<MainResponse>() {
@Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
int statusCode = response.code();
List<Schedule> schedule = response.body().getMonday();
recyclerView.setAdapter(new MainAdapter(schedule, R.layout.list_item_schedule, getApplicationContext()));
}
@Override
public void onFailure(Call<MainResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
如您所见,我使用 recycleview 来获取星期一数组的标题,但问题是当我运行应用程序时,它只是因为这个而崩溃:
尝试调用虚拟方法'java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()' 为空 对象引用
这是错误的整个对话框:
10-14 20:53:45.462 1050-1050/com.example.user_pc.capstonestage2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user_pc.capstonestage2, PID: 1050
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.example.user_pc.capstonestage2.MainResponse.getMonday()' on a null object reference
atcom.example.user_pc.capstonestage2.MainActivity$1.onResponse(MainActivity.java:36)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
我不明白为什么它是空的
我在logcat中也发现了这个错误:
10-14 21:02:12.373 9046-9046/com.example.user_pc.capstonestage2 E/RecyclerView:没有附加适配器;跳过布局
所以如果你需要适配器的代码,就是这样:
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MovieViewHolder> {
private List<Schedule> schedule;
private int rowLayout;
private Context context;
public static class MovieViewHolder extends RecyclerView.ViewHolder {
LinearLayout schedulesLayout;
TextView title;
public MovieViewHolder(View v) {
super(v);
schedulesLayout = (LinearLayout) v.findViewById(R.id.schedule_layout);
title = (TextView) v.findViewById(R.id.title);
}
}
public MainAdapter(List<Schedule> schedule, int rowLayout, Context context) {
this.schedule = schedule;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public MainAdapter.MovieViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new MovieViewHolder(view);
}
@Override
public void onBindViewHolder(MovieViewHolder holder, final int position) {
holder.title.setText(schedule.get(position).getTitle());
}
@Override
public int getItemCount() {
return schedule.size();
}
}
【问题讨论】:
-
你把 BASE_URL 写错了你在你的“v3”中留下了“v”你必须把它改成api.jikan.moe/v3