开发者社区> 问答> 正文

findviewbyid 空指针的问题

各位大侠,最近发现一个问题个人觉得非常诡异。

在oncreate 中 listView_test = (ListView) this.findViewById(R.id.listcardinfo); 为null。
但是 同一个页面中的一个button的 onClick方法确可以找到这个listview,并且赋值。

附上XML

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试"
            android:textColor="#000000"
            android:textSize="22.0dip" />

    <include layout="@layout/home_title_layout"/>
    </RelativeLayout>




    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

</LinearLayout>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listcardinfo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

展开
收起
爵霸 2016-06-12 10:09:03 1784 0
1 条回答
写回答
取消 提交回答
  • 你的list 在 LinearLayout 里面
    你应该先获取 到 LinearLayout 的 对象 然后 用 LinearLayout. findViewById();
    直接调用findViewById()使用的是setcontentview()获得的对象的findViewById方法。

    2019-07-17 19:33:16
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载