I am trying to make an app that finds all pdf in my phone, but the application crashes. I already got permission to write on storage but it didn't help. Here is my code:
package prototype.reader.com;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import java.util.*;
import java.io.*;
import android.widget.*;
public class lst extends Activity
{
ArrayList<String> bk;
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bk);
private ListView lv;
protected void onCreate(Bundle savedinstancestate){
super.onCreate(savedinstancestate);
setContentView(R.layout.listpdfs);
lv = findViewById(R.id.lpdf);
File sdCard = Environment.getExternalStorageDirectory();
recursiveFind(sdCard);
lv.setAdapter(ad);
}
public void recursiveFind(File file) {
File[] f = file.listFiles();
for (File ff : f) {
if (ff.isDirectory()) recursiveFind(file);
if (ff.isFile() && ff.getPath().endsWith(".pdf")) {
bk.add(ff.toString());
}
}
}
}
Try this you declare adapter wrong before arraylist add data you set adapter use below code
public class lst extends Activity {
ArrayList<String> bk;
ArrayAdapter<String> ad;
private ListView lv;
protected void onCreate(Bundle savedinstancestate) {
super.onCreate(savedinstancestate);
setContentView(R.layout.listpdfs);
bk = new ArrayList<>();
lv = findViewById(R.id.lpdf);
File sdCard = Environment.getExternalStorageDirectory();
recursiveFind(sdCard);
ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bk);
lv.setAdapter(ad);
}
public void recursiveFind(File file) {
File[] f = file.listFiles();
for (File ff : f) {
if (ff.isFile() && ff.getPath().endsWith(".pdf")) {
bk.add(ff.toString());
}
}
}
}
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.