我收到的错误在Android Studio中声明: java.lang.NullPointerException:尝试获取null数组的长度 代码在这里:
public class Main3Activity extends AppCompatActivity {
Button buttonOpenDialog;
Button buttonUp;
TextView textFolder;
String KEY_TEXTPSS ="TEXTPSS";
static final int CUSTOM_DIALOG_ID = 0;
ListView dialog_ListView;
File root;
File curFolder;
private List<String> fileList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
buttonOpenDialog = (Button) findViewById(R.id.findMusic);
buttonOpenDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(CUSTOM_DIALOG_ID);
}
});
root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
curFolder = root;
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case CUSTOM_DIALOG_ID:
dialog = new Dialog(Main3Activity.this);
dialog.setContentView(R.layout.dialoguelayout);
dialog.setTitle("Custom Dialog");
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
textFolder = (TextView) dialog.findViewById(R.id.folder);
buttonUp = (Button) dialog.findViewById(R.id.up);
buttonUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListDir(curFolder.getParentFile());
}
});
dialog_ListView = (ListView) dialog.findViewById(R.id.dialoglist);
dialog_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File selected = new File(fileList.get(position));
if(selected.isDirectory()) {
ListDir(selected);
} else {
Toast.makeText(Main3Activity.this, selected.toString() + " selected",
Toast.LENGTH_LONG).show();
dismissDialog(CUSTOM_DIALOG_ID);
}
}
});
break;
}
return dialog;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case CUSTOM_DIALOG_ID:
ListDir(curFolder);
break;
}
}
void ListDir(File f) {
if(f.equals(root)) {
buttonUp.setEnabled(false);
} else {
buttonUp.setEnabled(true);
}
curFolder = f;
textFolder.setText(f.getPath());
File[] files = f.listFiles();
fileList.clear();
for(File file : files) {
fileList.add(file.getPath());
}
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
dialog_ListView.setAdapter(directoryList);
}
}
这是错误:
08-16 15:20:05.085 30981-30981/com.example.letwixtor.myapplication2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.letwixtor.myapplication2, PID: 30981
java.lang.NullPointerException: Attempt to get length of null array
at com.example.letwixtor.myapplication2.Main3Activity.ListDir(Main3Activity.java:117)
at com.example.letwixtor.myapplication2.Main3Activity.onPrepareDialog(Main3Activity.java:99)
at android.app.Activity.onPrepareDialog(Activity.java:3380)
at android.app.Activity.showDialog(Activity.java:3444)
at android.app.Activity.showDialog(Activity.java:3394)
at com.example.letwixtor.myapplication2.Main3Activity$1.onClick(Main3Activity.java:43)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我认为错误来自“listFiles”函数,因为它无法返回null(即它没有找到目录。)我不太明白为什么它在root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
之后找不到该目录
运行模拟器和应用程序时,应用程序在尝试打开目录时崩溃。不知道如何解决这个问题。 提前谢谢。
答案 0 :(得分:0)
确保在清单中添加了<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
。