I get a "Unfortunately 'App Name' has stopped" message from the emulator. I think the script can't locate the folder or can't read the file name. The photo of the res folder is attached. I tried things like "res/drawable" or "/res/drawable/" etc but I got the same result. I used the below permission in the manifest file but to no avail.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I checked all stackoverflaw related links but could not solve my problem.
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);
}
}
}
}
My main activity file pls pay special attention to the path I gave for the 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());
}
}
}
}
This is the 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
You should fix the path you're trying to access, change this
File folder = new File("D:/Android_Dosyalar/Proje/PGNViewer/app/src/main/res/drawable");
to a path locate in the device, example:
File folder = new File("/data/chessactivepgnviewer.com.pgnviewer/");
Your problem is in these lines:
File folder = new File("D:/Android_Dosyalar/Proje/PGNViewer/app/src/main/res/drawable");
File[] listOfFiles = folder.listFiles();
as listOfFiles, it seems, null.
If you want to get a list of all your drawables, you might want to try this:
Field[] drawables = chessactivepgnviewer.com.pgnviewer.R.drawable.class.getFields();
for (Field f : drawables) {
try {
System.out.println("R.drawable." + f.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
The folder doesn't exists. So, folder.listFiles() return null.
If you put a picture inside the apk, use the assets folder for that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.