我正在使用Fragments和RecyclerView构建应用程序。
我正在关注本教程:THIS TUTORIAL
我正在使用教程的构建邮件片段部分来执行此操作。
但是我收到了这个错误:
08-17 10:21:51.674 32646-32646/zca.zcaimobile E/AndroidRuntime: FATAL EXCEPTION: main
Process: zca.zcaimobile, PID: 32646
java.lang.RuntimeException: Unable to start activity ComponentInfo{zca.zcaimobile/zca.zcaimobile.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at zca.zcaimobile.fragments.PaymentFragment.onViewCreated(PaymentFragment.java:43)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1244)
at android.app.Activity.performStart(Activity.java:6116)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我的PaymentFragment.java
package zca.zcaimobile.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import zca.zcaimobile.R;
import zca.zcaimobile.api.APIService;
import zca.zcaimobile.api.APIUrl;
import zca.zcaimobile.helper.PaymentAdapter;
import zca.zcaimobile.helper.SharedPrefManager;
import zca.zcaimobile.models.Payments;
public class PaymentFragment extends Fragment {
private RecyclerView recyclerViewMessages;
private RecyclerView.Adapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_messages, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Messages");
recyclerViewMessages = (RecyclerView) view.findViewById(R.id.recyclerViewMessages);
recyclerViewMessages.setHasFixedSize(true);
recyclerViewMessages.setLayoutManager(new LinearLayoutManager(getActivity()));
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Payments> call = service.getPayments(SharedPrefManager.getInstance(getActivity()).getUser().getId());
call.enqueue(new Callback<Payments>() {
@Override
public void onResponse(Call<Payments> call, Response<Payments> response) {
adapter = new PaymentAdapter(response.body().getMessages(), getActivity());
recyclerViewMessages.setAdapter(adapter);
}
@Override
public void onFailure(Call<Payments> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
我的fragment_payment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
我使用该教程的MessageFragment部分作为我的PaymentFragment.java的参考,因为该部分和我的paymentfragment具有相同的功能。
有人能帮助我吗?我是Android编程的新手,所以我真的不知道发生了什么。
谢谢。
答案 0 :(得分:1)
这是因为您的布局名称fragment_payment.xml
不是R.layout.fragment_messages
。 LayoutInflater 无法在fragment_messages.xml
改变这个:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_payment, container, false);
}