0
我想用我的應用程序來讀取SD卡的文件夾的內容,當我嘗試閱讀/mnt/secure
我得到以下錯誤:的Android空例外
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 /安全的文件夾我得到打印出來,我無法讀取該文件夾。我如何克服這一點? 謝謝
錯誤是在行中:ReadFileFOlderActivity.java:55,plz發佈此部分 – Nermeen
請更多您的活動代碼,以便我可以準確地瞭解您的邏輯。 – GrIsHu
我提供了完整的代碼,我相信問題出現的原因 – albertsmus