安卓系统:findViewById(R.id.listView)返回null



我有一个自定义布局:

taskview.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

我的活动如下:

DaysList.java

public class DaysList extends Activity {
    String []data= {"Wake up", "Take a shit", "Bath", "Feed your belly"};
    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);            
    }
    public void showTask(View view){
        listView= (ListView)findViewById(R.id.listView);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.task_row, R.id.textView, data);
        listView.setAdapter(adapter);
    }
}

showTask是一个侦听器函数,用于侦听onClick事件。我不知道为什么我得到findViewById(R.id.listview).的NullPointerException

顺便说一句,我是安卓系统的新手,这就是为什么我不确定我是否错过了什么。

编辑:我有两个布局,activity_main.xml有一些按钮。每当有人单击这些按钮时,就会调用showTask方法。

DaysList是与activity_main.xml关联的活动,因为它是我的应用程序的第一页。这就是为什么我不能用taskview.xml更改它。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#F44336"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".DaysList">

        <ImageView
            android:id="@+id/mon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginRight="5sp"
            android:src="@drawable/mon"
            android:adjustViewBounds="true"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="#F44336"
            android:fontFamily="sans-serif"
            android:paddingBottom="15sp"
            android:paddingTop="15sp"
            android:paddingLeft="25sp"
            android:text="Monday"
            android:textColor="#E8F5E9"
            android:textSize="34sp"
            android:onClick="showTask"/>
    </LinearLayout>

Thanks!!
setContentView(R.layout.activity_main);  

setContentView(R.layout.taskview);   

因为您设置了错误的xml文件。并在您的onCreate方法

中使用此方法

在onCreate方法中获取您的listView,但您还没有调用showTask作为

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    listView= (ListView)findViewById(R.id.listView); 
    showTask();          
}

并将其从showTask方法中删除

试试这个,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.taskview); 
    listView= (ListView)findViewById(R.id.listView);          
}

您的Listview位于taskview.xml 中

因此使用setContentView(R.layout.taskview);

使用showTask方法中给定的视图参数来查找您的id像这样的

listView= (ListView)view.findViewById(R.id.listView);

最新更新