Android:file.getName()错误

时间:2013-03-22 08:55:02

标签: android file filenames

我正在开发一项活动,以便浏览平板电脑中包含的文件,用户也可以选择其中一个并打开它。但是,当我检索文件名时,它说有一个NPE。我怎样才能检索文件名以便以后打开它? 这是我的代码:  文件资源管理器:

public class MainActivity extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
static String A;
static File file;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myPath = (TextView)findViewById(R.id.path);

    root = Environment.getExternalStorageDirectory().getPath();

    getDir(root);
}

private void getDir(String dirPath)
{
    myPath.setText("Location: " + dirPath);
    item = new ArrayList<String>();
    path = new ArrayList<String>();
    File f = new File(dirPath);
    File[] files = f.listFiles();

    if(!dirPath.equals(root))
    {
        item.add(root);
        path.add(root);
        item.add("../");
        path.add(f.getParent());    
    }

    for(int i=0; i < files.length; i++)
    {
        File file = files[i];

        if(!file.isHidden() && file.canRead()){
            path.add(file.getPath());
            if(file.isDirectory()){
                item.add(file.getName() + "/");
            }else{
                item.add(file.getName());
            }
        }   
    }

    ArrayAdapter<String> fileList =
            new ArrayAdapter<String>(this, R.layout.row, item);
    setListAdapter(fileList);   
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead()){
            getDir(path.get(position));
        }else{
            new AlertDialog.Builder(this)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("[" + file.getName() + "] folder can't be read!")
                .setPositiveButton("OK", null).show();  
        }   
    }else {
            file.getName();
            XMLPullParserActivity.Parsear(this);
                Intent i = new Intent(MainActivity.this, Correccion.class);
                 startActivity(i);



}

  }
public static String getA(){
    A = XMLPullParserHandler.getA();
    return A;
   }
public static String fileName(){
    return file.getName();
}
}

在活动中打开所选文件:

public class XMLPullParserActivity {

ListView listView;
public static  void Parsear(Context context){




    List<Puntuacion> puntuacion = null;
    try {
        XMLPullParserHandler parser = new XMLPullParserHandler();
        puntuacion = parser.parse(context.getAssets().open(MainActivity.fileName()));

    } catch (IOException e) {
        e.printStackTrace();
    }
}




}

logcat的:

03-22 09:57:44.296: W/ActivityThread(4943): Application com.example.androidexplorer can be debugged on port 8100...
03-22 09:57:44.398: D/AbsListView(4943): Get MotionRecognitionManager
03-22 09:57:44.507: D/libEGL(4943): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-22 09:57:44.523: D/libEGL(4943): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-22 09:57:44.523: D/libEGL(4943): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-22 09:57:44.640: D/OpenGLRenderer(4943): Enabling debug mode 0
03-22 09:58:02.656: D/AndroidRuntime(4943): Shutting down VM
03-22 09:58:02.656: W/dalvikvm(4943): threadid=1: thread exiting with uncaught exception (group=0x410a42a0)
03-22 09:58:02.679: E/AndroidRuntime(4943): FATAL EXCEPTION: main
03-22 09:58:02.679: E/AndroidRuntime(4943): java.lang.NullPointerException
03-22 09:58:02.679: E/AndroidRuntime(4943):     at com.example.androidexplorer.MainActivity.fileName(MainActivity.java:104)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at com.example.androidexplorer.XMLPullParserActivity.Parsear(XMLPullParserActivity.java:25)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at com.example.androidexplorer.MainActivity.onListItemClick(MainActivity.java:90)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3067)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.widget.AbsListView$1.run(AbsListView.java:3968)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.os.Handler.handleCallback(Handler.java:615)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.os.Looper.loop(Looper.java:137)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at android.app.ActivityThread.main(ActivityThread.java:4895)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at java.lang.reflect.Method.invoke(Method.java:511)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-22 09:58:02.679: E/AndroidRuntime(4943):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

你是initilize你的static File file。在每个方法中,您重新声明本地文件文件,因此您永远不会为类变量分配有效的引用。这里是NPE。可能

例如,在onListItemClick

File file = new File(path.get(position));

将隐藏类变量的声明

答案 1 :(得分:1)

文件为null,因为它未初始化,并且您无法在空指针上调用.getName()方法