weixin_45112339
chuck_023
采纳率50%
2019-10-07 09:12

在viewpager和fragment中嵌套listview,出现setAdapter空指针情况。

在viewpager和fragment中嵌套listview,出现setAdapter空指针情况。
为了实现滑动切换界面,我写了三个fragment,在一个fragment中加入了一个listview,怎么启动都是空指针。

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.example.tabhost_02.Fragment_Device.onActivityCreated(Fragment_Device.java:33)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2619)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManagerImpl.java:1696)
    at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:299)
    at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:235)
    at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
    at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
    at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    at android.view.View.measure(View.java:22002)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
    at android.view.View.measure(View.java:22002)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)

android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView5"
    android:layout_width="358dp"
    android:layout_height="wrap_content"
    android:text="状态详情"
    android:textColor="#000000"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="176dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="部件信息"
    android:textColor="#000000"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="温度传感器"
    android:textColor="#2196F3"
    android:textSize="18sp"
    android:textStyle="bold" />

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

public class Fragment_Device extends Fragment {
private List infos = new ArrayList<>();
private ListView device_info=null;
public Fragment_Device(){

}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_device, container, false);
    initInfo();
    return view;
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initInfo();
}
private void initInfo() {
    Info dev_1 = new Info("室内环境监控",
            "20摄氏度", "100小时", "2019-05-01 09:00:00");
    infos.add(dev_1);
    Info dev_2 = new Info("室内环境监控",
            "25摄氏度", "100小时", "2019-05-01 09:08:00");
    infos.add(dev_2);
    Info dev_3 = new Info("室内环境监控",
            "23摄氏度", "100小时", "2019-05-01 10:00:00");
    infos.add(dev_3);
    Info dev_4 = new Info("室内环境监控",
            "28摄氏度", "100小时", "2019-05-01 10:30:00");
    infos.add(dev_4);
    InfoAdapter infoAdapter = new InfoAdapter(infos,getContext());
    View view = LayoutInflater.from(getContext()).inflate(R.layout.list_item,null);
    infos=view.findViewById(R.id.lv);
    device_info = view.findViewById(R.id.lv);
    device_info.setAdapter(infoAdapter);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • weimingjue 王能 2年前

    onActivityCreated整个方法删掉
    initInfo整个方法的内容挪到onCreateView里面,并把View view = LayoutInflater.from(getContext()).inflate(R.layout.list_item,null);删掉即可

    学习的路还很长啊

    1.在Fragment的onActivityCreated里面初始化东西很容易崩溃,新手不要用这个方法。
    2.调用了2遍initInfo重复初始化。
    3.LayoutInflater等于重新new了一堆view,你在initInfo里用了但没有添加到任何控件上(没有返回给Fragment)
    4.R.layout.list_item里面的布局明显是adapter的,你却拿他来view.findViewById(R.id.lv)找listview,肯定找不到了,就算找到了,根据第三条错误你的页面也不会有任何反应。
    5.崩溃最简单的,直接找错误日志即可,以后记得先看错误日志,实在不会再把错误日志和对应代码发出来,新手博客:https://blog.csdn.net/weimingjue/article/details/87921494

    点赞 1 评论 复制链接分享
  • dabocaiqq dabocaiqq 2年前