ListView在被重置后返回空值

时间:2015-03-20 02:10:06

标签: java android listview

我搜索了SO,看起来像我这样的其他问题的解决方案似乎并不适合我。

我在setAdapter()上遇到NullPointerException。这是代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select_file);

    String currentDir = getApplicationContext().getFilesDir().getAbsolutePath();

    File file[] = GetFiles(currentDir);
    ArrayList<String> files = getFileNames(file);

    final ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, files);

    listView = (ListView)findViewById(android.R.id.list);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent myActivity = new Intent(SelectFile.this, MyActivity.class);
            myActivity.putExtra("filename", String.valueOf(listView.getItemAtPosition(position)));
            startActivity(myActivity);
            finish();
        }
    });
}

和XML:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="edu.niu.cs.jacob.journal.SelectFile"
android:id="@+id/relativeLay">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Select A File"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/list"
    android:layout_centerHorizontal="true"
    android:layout_below="@android:id/list"/>

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at edu.niu.cs.jacob.journal.SelectFile.onCreate(SelectFile.java:44)
    at android.app.Activity.performCreate(Activity.java:6221)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

我已经将它缩小到ListView在使用findViewById初始化时仍为null的事实。我已经检查过以确保它引用了正确XML文件中的正确元素。还有什么可能导致这个问题?

2 个答案:

答案 0 :(得分:1)

这用于ListActivity android.R.id.list

更改:

listView = (ListView)findViewById(android.R.id.list);

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

请参阅此类似问题:Your content must have a ListView whose id attribute is 'android.R.id.list'

答案 1 :(得分:1)

这是因为您没有使用自己的R。将findViewById更改为

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