gpt4 book ai didi

java - 使用 Android Studio 创建的应用程序在尝试打开文件夹时崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:36 25 4
gpt4 key购买 nike

我在 Android Studio 中收到一条错误消息:java.lang.NullPointerException:尝试获取空数组的长度代码在这里:

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()); 之后找不到目录

运行模拟器和应用程序时,应用程序在尝试打开目录时崩溃。不知道如何解决这个问题。提前致谢。

最佳答案

确保您添加了 <uses-permission android:name="androi‌​d.permission.READ_EXT‌​ERNAL_STORAGE" />在你的 list 中。

关于java - 使用 Android Studio 创建的应用程序在尝试打开文件夹时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38978979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com