我從仿真器中收到一個「不幸」的應用程序名稱「已停止」消息。我認爲腳本找不到文件夾或無法讀取文件名。 res文件夾的照片已附加。我嘗試了像「res/drawable」或「/ res/drawable /」等,但我得到了相同的結果。 我在清單文件中使用了下面的權限,但無濟於事。java.lang.NullPointerException:嘗試獲取與'無法讀取空數組'有關的空數組的長度
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我檢查了所有stackoverflaw相關的鏈接,但無法解決我的問題。
package chessactivepgnviewer.com.pgnviewer;
import java.io.File;
import java.util.ArrayList;
/**
* Created by serhat on 04.11.2015.
*/
public class klasor {
private String drawable;
private ArrayList<File> files;
public void listf(String drawable, ArrayList<File> files) {
File directory;
directory = new File(drawable);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
files.add(file);
} else if (file.isDirectory()) {
listf(file.getAbsolutePath(), files);
}
}
}
}
我的主要活動文件請特別注意我給了文件的路徑
package chessactivepgnviewer.com.pgnviewer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.satranc_tahtasi);
File folder = new File("D:/Android_Dosyalar/Proje/PGNViewer/app/src/main/res/drawable");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
}
這是logcat的
11-05 22:29:41.577 2215-2215/? I/art: Not late-enabling -Xcheck:jni (already on)
11-05 22:29:41.678 2215-2215/chessactivepgnviewer.com.pgnviewer D/AndroidRuntime: Shutting down VM
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: FATAL EXCEPTION: main
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: Process: chessactivepgnviewer.com.pgnviewer, PID: 2215
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{chessactivepgnviewer.com.pgnviewer/chessactivepgnviewer.com.pgnviewer.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5257)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to get length of null array
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at chessactivepgnviewer.com.pgnviewer.MainActivity.onCreate(MainActivity.java:20)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5990)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-05 22:29:41.679 2215-2215/chessactivepgnviewer.com.pgnviewer
謝謝你的回答。我會檢查並返回...問候 - 阿齊茲S. Kural –
你有權限清單文件中的READ_EXTERNAL_STORAGE? – Alisson
很抱歉,我把我的答案放在了錯誤的地方。 Regards –