Fragment中的RecyclerView无法找到 - null对象引用

时间:2016-05-13 12:59:18

标签: java android json android-fragments

我使用本教程https://www.learn2crack.com/2016/02/recyclerview-json-parsing.html来解析我的应用程序中的JSON。我在Android Studio中使用了选项卡式模板。我添加了两个片段,VFragment应该显示JSON。我在VFragment中添加了MainActivity教程中的代码(在教程中)。我的fragment_v布局也有recyclerview元素。

VFragment.java:

package com.tomwellmann.apptodate;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class VFragment extends Fragment {
    private RecyclerView recyclerView;
    private ArrayList<AndroidVersion> data;
    private DataAdapter adapter;

    public VFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        initViews();
        return inflater.inflate(R.layout.fragment_v, container, false);
    }

    private void initViews(){
        recyclerView = (RecyclerView)getActivity().findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
    }

    private void loadJSON(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.learn2crack.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        call.enqueue(new Callback<JSONResponse>() {

            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                JSONResponse jsonResponse = response.body();
                data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
                adapter = new DataAdapter(data);
                recyclerView.setAdapter(adapter);
            }

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

Logcat:

Process: com.tw.a, PID: 6653 java.lang.NullPointerException: 
Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' 
on a null object reference at com.tw.a.VFragment.initViews(VFragment.java:46)

1 个答案:

答案 0 :(得分:1)

问题在这里

return inflater.inflate(R.layout.fragment_v, container, false);

你可以试试这个

 View RootView;//Global
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) { 

         RootView = inflater.inflate(R.layout.fragment_v, container, false);
         initViews();
         return RootView;
    }

     private void initViews(){
            recyclerView = (RecyclerView)RootView.findViewById(R.id.card_recycler_view);
            recyclerView.setHasFixedSize(true);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
            recyclerView.setLayoutManager(layoutManager);
            loadJSON();
        }

您应该访问的更多细节

Why findViewById() doesn't work as getActivity().findViewById() here?