【问题标题】:ArrayAdapter inside Tablayout using fragmentsTablayout 中的 ArrayAdapter 使用片段
【发布时间】:2017-10-14 19:12:06
【问题描述】:

所以基本上我试图在一个片段内有一个 ArrayAdapter 用于 TabLayout

public class fragment_restaurants extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_restaurants, container, false);

        ArrayList<InformationModel> hotelList = new ArrayList<>();

        hotelList.add(new InformationModel("Copacabana Palace", "Copacabana", "215"));
        hotelList.add(new InformationModel("Copacabana Resort", "Botafogo", "589"));
        hotelList.add(new InformationModel("Sarrada Motel", "Bangu", "105"));
        hotelList.add(new InformationModel("5 Estrelas da Oi", "Centro", "669"));
        hotelList.add(new InformationModel("Pereira Cardoso", "Saramamduba", "123"));
        hotelList.add(new InformationModel("Sepetiba Palace", "Sepetiba", "774"));
        hotelList.add(new InformationModel("Copacabana Palace", "Copacabana", "215"));
        hotelList.add(new InformationModel("Copacabana Resort", "Botafogo", "589"));
        hotelList.add(new InformationModel("Sarrada Motel", "Bangu", "105"));
        hotelList.add(new InformationModel("5 Estrelas da Oi", "Centro", "669"));
        hotelList.add(new InformationModel("Pereira Cardoso", "Saramamduba", "123"));
        hotelList.add(new InformationModel("Sepetiba Palace", "Sepetiba", "774"));

        InformationAdapter adapter = new InformationAdapter(mContext, hotelList);
        ListView listView = (ListView) view.findViewById(R.id.listViewTabRestaurants);

        listView.setAdapter(adapter);
        return view;
    }
}

我遇到了这个错误:

致命异常:主要 进程:com.example.rick.tourapp,PID:4616 java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' 为空 对象引用

我该如何解决这个问题?我应该用这个数组适配器创建另一个类并在这个片段子类中初始化吗?

【问题讨论】:

    标签: android android-fragments android-arrayadapter


    【解决方案1】:

    查看您的代码似乎您的 mContext 为空

    初始化它:

    mContext = getActivity();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多