2013-08-24 32 views
0

嗨我嘗試從外部stroge獲取文件名,但我有一個錯誤,哪裏是問題?android null指針錯誤

public class MainActivity extends Activity { 

//protected ContextWrapper context; 

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

    Button test=(Button) findViewById(R.id.button1); 
    test.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //ContextWrapper context = new ContextWrapper(); 
      // TODO Auto-generated method stub 
     // File datapath = Environment.getDataDirectory(); 
      //File cacheDir = new File(context.getCacheDir(),"tempfile"); 
      String extState = new String(); 
      extState=Environment.getExternalStorageState(); 
      //File amcuk=Environment.getExternalStorageDirectory(); 
      //String externalname=new String(); 
      //String internalname=new String(); 
      //you may also want to add (...|| Environment.MEDIA_MOUNTED_READ_ONLY) 
      //if you are only interested in reading the filesystem 
      //try { 
      String name=new String(); 
       if(!extState.equals(Environment.MEDIA_MOUNTED)) { 
        //handle error here 
        Log.d("ss", "sad");} 
       else { 
        //File f=new File(extState); 
        Log.d("www", "www"); 
        Log.d("wwwaaaaaaaaaaaaaaa", "wwwaaaaaaaaaaaaa"); 
        File sdCardRoot = Environment.getExternalStorageDirectory(); 
        File yourDir = new File(sdCardRoot, "yourpath"); 
        for (File f : yourDir.listFiles()) { 
         if (f.isFile()){ 
          name = f.getName(); 
          } 
          // make something with the name 
        } 
      }// catch (Exception e) { 
       // TODO: handle exception 
       //Log.d("sasd", "sad");*/ 
      //} 

      Log.d("dosya adları", extState); 
      //File mydataDir =new File(path,"path"); 



      //Log.d("dosya adları internal", internalname); 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

附加的manifest.xml權限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

DDMS日誌消息是

08-24 16:54:38.986: E/AndroidRuntime(2629): FATAL EXCEPTION: main 
08-24 16:54:38.986: E/AndroidRuntime(2629): java.lang.NullPointerException 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at com.example.fileexplorear.MainActivity$1.onClick(MainActivity.java:55) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at android.view.View.performClick(View.java:2461) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at android.view.View$PerformClick.run(View.java:8890) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at android.os.Handler.handleCallback(Handler.java:587) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at android.os.Looper.loop(Looper.java:123) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at android.app.ActivityThread.main(ActivityThread.java:4632) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-24 16:54:38.986: E/AndroidRuntime(2629):  at dalvik.system.NativeStart.main(Native Method) 

我55行是

     for (File f : yourDir.listFiles()) { 

回答

0

唯一可能的解釋,我可以看到的是, listFiles()是返回null。

the docs

返回包含在由 此文件所表示的目錄中的文件的陣列。結果是null如果此文件不是目錄。如果文件 的路徑是絕對路徑,則數組中文件的路徑是絕對路徑,否則它們是相對路徑。

返回

文件或空的陣列。

因此,您的文件不是目錄。

你可以嘗試使用只有一個字符串參數的DIR而不是

創建文件(並跳過你"yourpath"參數):

String sdCardRoot = Environment.getExternalStorageDirectory().toString(); 
    File yourDir = new File(root_sd) ;  
    File list[] = yourDir.listFiles(); 

constructs a new file using the specified path

+0

感謝它的作品,但只寫了一個文件名在哪裏?h – amra

+0

@amra不確定。你是否100%肯定它在你所列的目錄中? – keyser

+0

@Keyser我不知道,因爲我打印文件只顯示此消息顯示[Ljava.io.File; @ 47f42770 – amra