chuck_023 2019-10-07 01:12 采纳率: 0%
浏览 707
已采纳

在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条回答 默认 最新

  • 王能 2019-10-08 01:36
    关注

    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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥20 小规模孤立词识别系统设计
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?
  • ¥15 Linux扩容时,格式化卡住了:vgdispaly查看卷组信息,没有输出
  • ¥18 关于#ubuntu#的问题:使用背景-工作职责内有七八台ubuntu系统的电脑,平时需要互相调取资料,想实现把这几台电脑用交换机组成一个局域网,来实现指定文件夹的互相调取和写入
  • ¥20 求一个简易射频信号综测仪
  • ¥15 esp8266 tally灯 接收端改为发射端
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部