android null指针错误

时间:2013-08-24 14:14:15

标签: java android eclipse nullpointerexception

嗨,我尝试从外部stroge获取文件名,但我有一个错误,哪里有问题?

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()) {

1 个答案:

答案 0 :(得分:0)

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

来自the docs

  

返回包含在由...表示的目录中的文件数组   这个文件。如果此文件不是目录,则结果为 null 。该   如果此文件的路径,则数组中文件的路径是绝对的   是绝对的,否则它们是相对的。

     

<强>返回

     

文件数组或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