0

嘿大家现在我正试图在 android 设备 sd 卡上显示某个文件夹中的所有文件,ListView到目前为止我运气不好,它因空指针异常而崩溃,所以我想知道这里是否有人可以发光我的问题将我的活动和 logcat 贴在下面,谢谢。

活动

package com.mkyong.android;

import java.io.File;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class ListMobileActivity extends ListActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.res);

    }
        public ArrayList<String> GetFiles(String DirectoryPath) {
            ArrayList<String> MyFiles = new ArrayList<String>();
            File f = new File(DirectoryPath);

            f.mkdirs();
            File[] files = f.listFiles();
            if (files.length == 0)
                return null;
            else {
                for (int i=0; i<files.length; i++) 
                    MyFiles.add(files[i].getName());
            }

            return MyFiles;
        }
        {
        ListView lv;
        ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox");
        lv = (ListView)findViewById(R.id.listView1);

        lv.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, FilesInFolder));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

             }
        });
    }




    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}

和日志猫

05-24 06:42:28.992: E/AndroidRuntime(796): FATAL EXCEPTION: main
05-24 06:42:28.992: E/AndroidRuntime(796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mkyong.android/com.mkyong.android.ListMobileActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-24 06:42:28.992: E/AndroidRuntime(796):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-24 06:42:28.992: E/AndroidRuntime(796):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-24 06:42:28.992: E/AndroidRuntime(796):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-24 06:42:28.992: E/AndroidRuntime(796):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-24 06:42:28.992: E/AndroidRuntime(796):  at android.os.Handler.dispatchMessage(Handler.java:99)
4

3 回答 3

1

如果您已在 android manifest 中请求读取 storage 的权限,那么这可能是问题所在。

从代码看来,您正在查询实例块中的列表视图实例。这可能会在调用 setContentView 之前调用。在布局膨胀后,您应该在 XML 中实例化 UI 组件,否则您将遇到异常。

试试这个。

    public class ListMobileActivity extends ListActivity {

    ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.res);

        ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox");
        lv = (ListView) findViewById(R.id.listView1);

        lv.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, FilesInFolder));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }
        });
    }

    public ArrayList<String> GetFiles(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File f = new File(DirectoryPath);

        f.mkdirs();
        File[] files = f.listFiles();
        if (files.length == 0)
            return null;
        else {
            for (int i = 0; i < files.length; i++)
                MyFiles.add(files[i].getName());
        }

        return MyFiles;
    }

}

它应该工作。

于 2013-05-24T06:41:29.003 回答
0

如日志:

ListMobileActivity}:java.lang.RuntimeException:您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView

如果要扩展 ListActivity 以创建 ListView,则需要将 xml 中的 ListView id 设置为“android.R.id.list”。做它作为res布局:

<ListView android:id="@id/android:list" <!--set ListView id to android.R.id.list-->
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:scrollbars="vertical"/>

并在代码中将 ListView 实例初始化为:

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

或者

lv =ListMobileActivity.this.getListView();
于 2013-05-24T06:41:39.183 回答
0

试试这个:

package com.mkyong.android;

import java.io.File;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;

public class ListMobileActivity extends ListActivity {

    ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.res);

        ArrayList<String> FilesInFolder = GetFiles("/sdcard/RootBox");

        setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, FilesInFolder));
    }

    public ArrayList<String> GetFiles(String DirectoryPath) {
        ArrayList<String> MyFiles = new ArrayList<String>();
        File f = new File(DirectoryPath);

        f.mkdirs();
        File[] files = f.listFiles();
        if (files.length == 0)
            return null;
        else {
            for (int i=0; i<files.length; i++){
                MyFiles.add(files[i].getName());
            }
        }

        return MyFiles;
    }    

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();

    }

}
于 2013-05-24T06:49:28.093 回答