Android显示SD卡文件抛出java.lang.NullPointerException

时间:2016-07-01 19:21:57

标签: java android

这是MainActivity文件。当我在模拟器和手机中运行它时,logcat会因NullPointerException而显示崩溃。我已经阅读了很多关于不让用户或其他活动将“null”值传递给我的方法,但无法解决这个问题。

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        //I used a listview with id= "filelist" in layout 
        ListView lv;
        ArrayList<String> FilesInFolder;
        FilesInFolder =    GetFiles(Environment.getExternalStorageDirectory().getPath()+ "/sdcard/");
        lv = (ListView)findViewById(R.id.filelist);
        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, FilesInFolder);

        lv.setAdapter(listAdapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Clicking on items
            }
        });

    }
    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;
    }
}

这是logcat:

07-02 01:09:40.500 4407-4407/com.amenhotep.filelister W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
07-02 01:09:40.501 4407-4407/com.amenhotep.filelister E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.amenhotep.filelister, PID: 4407
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amenhotep.filelister/com.amenhotep.filelister.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
        at android.app.ActivityThread.access$900(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5345)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at com.amenhotep.filelister.MainActivity.GetFiles(MainActivity.java:44)
        at com.amenhotep.filelister.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:5343)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441) 
        at android.app.ActivityThread.access$900(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:5345) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
        at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

问题出在您的SD卡路径中,使用此代码获取SD卡的路径 String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();

并且无需向其添加"/sdcard/",也可以更改GetFiles方法,并删除mkdirs(),因为之前存在sdcard

看一下这个例子

    File sdcard_files_and_folders[] = new File(DIR_SDCARD).listFiles();
    for (File fileOrFolder: sdcard_files_and_folders) {
        // do any thing that you want, add them to list or...
        Log.i("FILE", fileOrFolder.toString());
    }