我试图读取sdCard的文件夹内容与我的应用程序,当我试图读取/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'的内容时,我得到了之前提供的错误。
所以环顾四周,我发现我不能读取文件夹,因为读取权限?我将上面的代码更改为:
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: ");
}
}
因此,当我进入/mnt/secure文件夹时,我打印出无法读取该文件夹。我怎样才能克服呢?由于
显然,您还没有找到textView(Text)。还有,请不要用大写字母来命名局部变量。