Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试读取文件夹';内容_Android_File - Fatal编程技术网

Android 尝试读取文件夹';内容

Android 尝试读取文件夹';内容,android,file,Android,File,我试图用我的应用程序读取SD卡的文件夹内容,当我试图读取/mnt/secure时,我收到以下错误: 01-21 11:54:03.180: D/AndroidRuntime(3080): Shutting down VM 01-21 11:54:03.180: W/dalvikvm(3080): threadid=1: thread exiting with uncaught exception (group=0x40028a00) 01-21 11:54:03.190: E/AndroidRu

我试图用我的应用程序读取SD卡的文件夹内容,当我试图读取
/mnt/secure
时,我收到以下错误:

01-21 11:54:03.180: D/AndroidRuntime(3080): Shutting down VM
01-21 11:54:03.180: W/dalvikvm(3080): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
01-21 11:54:03.190: E/AndroidRuntime(3080): FATAL EXCEPTION: main
01-21 11:54:03.190: E/AndroidRuntime(3080): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pas.readfilefolder/com.pas.readfilefolder.ReadFileFOlderActivity}: java.lang.NullPointerException
01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.os.Looper.loop(Looper.java:143)
 01-21 11:54:03.190: E/AndroidRuntime(3080):    at android.app.ActivityThread.main(ActivityThread.java:4914)
 01-21 11:54:03.190: E/AndroidRuntime(3080):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-21 11:54:03.190: E/AndroidRuntime(3080):   at java.lang.reflect.Method.invoke(Method.java:521)
 01-21 11:54:03.190: E/AndroidRuntime(3080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 01-21 11:54:03.190: E/AndroidRuntime(3080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 01-21 11:54:03.190: E/AndroidRuntime(3080):    at dalvik.system.NativeStart.main(Native Method)
 01-21 11:54:03.190: E/AndroidRuntime(3080): Caused by: java.lang.NullPointerException
01-21 11:54:03.190: E/AndroidRuntime(3080):     at com.pas.readfilefolder.ReadFileFOlderActivity.displayListView(ReadFileFOlderActivity.java:55)
  01-21 11:54:03.190: E/AndroidRuntime(3080):   at com.pas.readfilefolder.ReadFileFOlderActivity.onCreate(ReadFileFOlderActivity.java:45)
   01-21 11:54:03.190: E/AndroidRuntime(3080):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
    01-21 11:54:03.190: E/AndroidRuntime(3080):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
    01-21 11:54:03.190: E/AndroidRuntime(3080):     ... 11 more
    01-21 11:54:05.580: I/Process(3080): Sending signal. PID: 3080 SIG: 9
我使用的代码是:

package com.pas.readfilefolder;

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




import android.R.bool;
import android.app.ListActivity;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ReadFileFOlderActivity extends ListActivity {
/** Called when the activity is first created. */
 private ArrayList<String> todoItems;
 private TextView label;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);        
    label=(TextView)findViewById(R.id.label);

    File[] fileMyList = Environment.getExternalStorageDirectory().listFiles(); 



    Log.d("mes2",Environment.getExternalStorageDirectory().toString());
    Button btn = (Button) findViewById(R.id.btnAdd);
    displayListView(fileMyList);   


}

private void displayListView(File[] fileMyList) {
    todoItems = new ArrayList<String>();

    String nameOfFolder="";
    for(int i=0;i<fileMyList.length;i++)
        {Log.d("",fileMyList[i].getName()); 



        nameOfFolder=fileMyList[i].getParent();





        if(fileMyList[i].isDirectory()){
            todoItems.add(fileMyList[i].getName());
        }
    }

    //Folder[] MyfolderList=Environment.getExternalStorageDirectory().isDirectory(); 
    setContentView(R.layout.main);
    TextView TextFolder=(TextView)findViewById(R.id.label);
    TextFolder.setText(nameOfFolder);
    setListAdapter(new IconicAdapter());
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);          
    String onomaBashs =  settings.getString(FullFIleName, "");

    if (onomaBashs.endsWith(".sqlite")) {

        TextView Text=(TextView)findViewById(R.id.label);
        Text.setText(onomaBashs);

    } else {
        //throw new IllegalArgumentException("Unsupported connection string");
    }
}

public void OnClickButton(View view) {


    TextView TextFolder=(TextView)findViewById(R.id.label);

    File directory = new File(TextFolder.getText().toString());

    if (directory.toString().matches("/"))
    {

        return;
    }
    else{
        directory=new File(directory.getParent());
        Log.d("gonikos yparxei",directory.toString());
        File[] fileMyList = directory.listFiles();
        displayListView(fileMyList);
    }       
}

private static final String FullFIleName = "Onoma_Bashs";
private static final String PREFS_NAME = "NamesOfDatabases";

public void onListItemClick(ListView parent, View v,
        int position, long id) {
    TextView Text=(TextView)findViewById(R.id.label);
    String fdrName=Text.getText().toString();
    File file = new File(fdrName+"/"+todoItems.get(position).toString());
    System.out.println(file.toString());  

    if(file.isDirectory()){     
        if(file.list().length>0){     
            System.out.println("Directory is not empty!");     
        }else{
            System.out.println("Directory is empty!");
            }

    }else{
            System.out.println("This is not a directory");
        }       
}



class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
    super(ReadFileFOlderActivity.this, R.layout.listfilefolder, R.id.label, todoItems);
    }
    public View getView(int position, View convertView,
            ViewGroup parent) {
            View row=convertView;
            if (row==null) {
                LayoutInflater inflater=getLayoutInflater();
                row=inflater.inflate(R.layout.listfilefolder, parent, false);
            }
            TextView label=(TextView)row.findViewById(R.id.label);
            label.setText(todoItems.get(position));
            ImageView icon=(ImageView)row.findViewById(R.id.icon);
            if (todoItems.get(position).length()>4) {
                icon.setImageResource(R.drawable.folder);
            }
            else {
                icon.setImageResource(R.drawable.ic_launcher);
            }
            return(row);
        }
    }
}
因此,当我转到/mnt/secure文件夹时,我被打印出来,无法读取该文件夹。我怎样才能克服这个问题?
显然,您还没有找到文本视图(Text),谢谢。请停止使用大写字母命名局部变量。

错误在以下行:ReadFileFOlderActivity.java:55,请发布此部分请提供更多活动代码,以便我能准确了解您的逻辑。我提供了完整代码,我相信问题发生的原因
    public void onListItemClick(ListView parent, View v,
        int position, long id) {
    TextView Text=(TextView)findViewById(R.id.label);
    String fdrName=Text.getText().toString();
    File file = new File(fdrName+"/"+todoItems.get(position).toString());
    System.out.println(file.toString());  
    if (file.canRead()) {
        System.out.println(file.getAbsolutePath() + "Can Read: ");
        if(file.isDirectory()){     
            if(file.list().length>0){     
                System.out.println("Directory is not empty!");     
            }else{
                System.out.println("Directory is empty!");
                }

        }else{
                System.out.println("This is not a directory");
        }   
    } else {
        System.out.println(file.getAbsolutePath() + " Cannot Read: ");
    }

}