2016-07-01 97 views
0

這是MainActivity文件。當我在模擬器和手機中運行它時,由於NullPointerException,logcat顯示崩潰。我讀了很多關於不讓用戶或其他活動向我的方法傳遞「空值」的方法,但無法解決此問題。Android顯示SD卡文件拋出java.lang.NullPointerException

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) 
+0

分享,以便在logcat中,我們可以幫你! – W0rmH0le

+0

請不要將變量大寫爲EVER。它使你的代碼難以閱讀。 –

+0

你在你的Androidmanifest.xml中設置了權限嗎? babadaba

回答

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()); 
    } 
+0

我做了什麼你說:1-'String DIR_SDCARD = Environment.getExternalStorageDirectory()。getAbsolutePath(); ArrayList FilesInFolder; FilesInFolder = GetFiles(「DIR_SDCARD」);''刪除'mkdirs()'。仍然我得到相同的異常 – Amenhotep

+0

'GetFiles(「DIR_SDCARD」)'是錯誤的,使用'GetFiles(DIR_SDCARD)' –

+0

該死!工作! – Amenhotep

相關問題