Java - Null Object RecyclerView.SetAdapter Android

时间:2017-04-06 07:40:26

标签: java android nullpointerexception android-recyclerview

我在android中有错误显示fragment.this错误来自setAdapater recyclerView。 错误是

instance of

这是我的片段类:

04-06 14:24:14.875 24405-24405/com.v1.milzam.cuti E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.v1.milzam.cuti, PID: 24405
                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
                                                                    at com.v1.milzam.cuti.Fragment.Fragment_Cuti_List$1.onResponse(Fragment_Cuti_List.java:97)
                                                                    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                                    at android.os.Handler.handleCallback(Handler.java:815)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                    at android.os.Looper.loop(Looper.java:210)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5833)
                                                                    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:1113)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

这个适配器:

public class Fragment_Cuti_List extends Fragment implements View.OnClickListener {
private RecyclerView recyclerView;
private SharedPreferences pref;
private LinearLayout linearLayout;
private ArrayList<Leave> data;
private CutiAdapter adapt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.frame_cuti_list,container,false);
    initView(view);
    return view;
}

@Override
public void onClick(View v) {

}

private void initView(View view){
    linearLayout=(LinearLayout) view.findViewById(R.id.linearCuti);
    pref=view.getContext().getSharedPreferences("context",0);
    RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerCuti2);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    loadCuti();
}

private void loadCuti(){
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    // add your other interceptors …
    // add logging as last interceptor
    httpClient.addInterceptor(logging);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();
    RequestInterface requestInterface=retrofit.create(RequestInterface.class);
    Leave cuti=new Leave();
    String id=pref.getString(Constants.EMP_ID,"");
    cuti.setId(id);
    ServerRequest request=new ServerRequest();
    request.setOperation(Constants.SELECT);
    request.setCuti(cuti);
    Call<ServerResponse> response = requestInterface.operation(request);
    response.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
            ServerResponse resp = response.body();
            Snackbar.make(linearLayout,resp.getMessage(),Snackbar.LENGTH_LONG).show();
            if(resp.getResult().equals(Constants.SUCCESS)){
                data = new ArrayList<>(Arrays.asList(resp.getCuti()));
                adapt=new CutiAdapter(getActivity(),data);
                recyclerView.setAdapter(adapt);
            }


        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
            Log.d("Error",t.getMessage());
        }
    });
}}

来自服务器的json请求成功。

1 个答案:

答案 0 :(得分:2)

替换此行

RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerCuti2);

recyclerView = (RecyclerView)view.findViewById(R.id.recyclerCuti2);

问题是您已经声明了一个全局的RecyclerView对象,但是从未在创建新的本地RecycerView对象时对其进行初始化