可能这是重复的问题,但我没有找到任何解决方案。
我想从文件夹中获取文件名并使用async显示它但我收到以下错误FATAL EXCEPTION: AsyncTask #1 Error java.lang.RuntimeException: An error occured while executing doInBackground()
以下是我的代码
private static String[] listArr ;
private static String[] listArrview ;
private static final ArrayList<String> ary_user_pdf = new ArrayList<String>();
private static final ArrayList<String> ary_user_pdfview = new ArrayList<String>();
new AsyncAction().execute(null, null, null);
filldata()函数
if(file.exists() && file.listFiles().length>0)
{
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
//myList.add( list[i].getName() );
ary_user_pdf.add(list[i].getName());
ary_user_pdfview.add(list[i].getName());
}
listArr = new String[ary_user_pdf.size()];
listArr = ary_user_pdf.toArray(listArr);
listArrview = new String[ary_user_pdfview.size()];
listArrview = ary_user_pdfview.toArray(listArrview);
}
private class AsyncAction extends AsyncTask<String, Void, String> {
public boolean status = false;
private ProgressDialog pd;
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
fillData();
status = true;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
@Override
protected void onPostExecute(String result) {
pd.dismiss();
MyArrayAdapter adapter = new MyArrayAdapter(Downloadpdf.this,
listArr);
lstSub.setAdapter(adapter);
}
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(Downloadpdf.this);
pd.setTitle("PDF");
pd.setMessage("Please wait while we are loading PDF.");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
LOG:
10-10 13:05:19.791: E/AndroidRuntime(19301): FATAL EXCEPTION: AsyncTask #1
10-10 13:05:19.791: E/AndroidRuntime(19301): java.lang.RuntimeException: An error occured while executing doInBackground()
10-10 13:05:19.791: E/AndroidRuntime(19301): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.lang.Thread.run(Thread.java:1019)
10-10 13:05:19.791: E/AndroidRuntime(19301): Caused by: java.lang.NoSuchMethodError: android.os.Bundle.getString
10-10 13:05:19.791: E/AndroidRuntime(19301): at com.paper.solution.Downloadpdf.fillData(Downloadpdf.java:162)
10-10 13:05:19.791: E/AndroidRuntime(19301): at com.paper.solution.Downloadpdf$AsyncAction.doInBackground(Downloadpdf.java:112)
10-10 13:05:19.791: E/AndroidRuntime(19301): at com.paper.solution.Downloadpdf$AsyncAction.doInBackground(Downloadpdf.java:1)
10-10 13:05:19.791: E/AndroidRuntime(19301): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-10 13:05:19.791: E/AndroidRuntime(19301): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-10 13:05:19.791: E/AndroidRuntime(19301): ... 4 more
10-10 13:05:20.841: E/WindowManager(19301): Activity com.paper.solution.Downloadpdf has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4052ee20 that was originally added here
10-10 13:05:20.841: E/WindowManager(19301): android.view.WindowLeaked: Activity com.paper.solution.Downloadpdf has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4052ee20 that was originally added here
10-10 13:05:20.841: E/WindowManager(19301): at android.view.ViewRoot.<init>(ViewRoot.java:258)
10-10 13:05:20.841: E/WindowManager(19301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-10 13:05:20.841: E/WindowManager(19301): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-10 13:05:20.841: E/WindowManager(19301): at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.Dialog.show(Dialog.java:241)
10-10 13:05:20.841: E/WindowManager(19301): at com.paper.solution.Downloadpdf$AsyncAction.onPreExecute(Downloadpdf.java:151)
10-10 13:05:20.841: E/WindowManager(19301): at android.os.AsyncTask.execute(AsyncTask.java:391)
10-10 13:05:20.841: E/WindowManager(19301): at com.paper.solution.Downloadpdf.layout(Downloadpdf.java:98)
10-10 13:05:20.841: E/WindowManager(19301): at com.paper.solution.Downloadpdf.onCreate(Downloadpdf.java:71)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
10-10 13:05:20.841: E/WindowManager(19301): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 13:05:20.841: E/WindowManager(19301): at android.os.Looper.loop(Looper.java:123)
10-10 13:05:20.841: E/WindowManager(19301): at android.app.ActivityThread.main(ActivityThread.java:3647)
10-10 13:05:20.841: E/WindowManager(19301): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 13:05:20.841: E/WindowManager(19301): at java.lang.reflect.Method.invoke(Method.java:507)
10-10 13:05:20.841: E/WindowManager(19301): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-10 13:05:20.841: E/WindowManager(19301): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-10 13:05:20.841: E/WindowManager(19301): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您正在调用Bundle.getString(String, String),该{{3}}仅在没有API 12的设备上添加到API 12中。
确保对属于API的方法的调用高于您的minSdk值进行了正确定位(使用@TargetApi注释),或者不使用它们,并且仅使用API中添加的Bundle.getString(String)
方法1。
答案 1 :(得分:0)
这样做
new AsyncAction().execute();
然后在异步任务中
private class AsyncAction extends AsyncTask<Void, Void, Void> {
public boolean status = false;
private ProgressDialog pd;
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
fillData();
status = true;
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
@Override
protected void onPostExecute(Void result) {
pd.dismiss();
MyArrayAdapter adapter = new MyArrayAdapter(Downloadpdf.this,
listArr);
lstSub.setAdapter(adapter);
}
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(Downloadpdf.this);
pd.setTitle("PDF");
pd.setMessage("Please wait while we are loading PDF.");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
答案 2 :(得分:0)
检查是否已实例化文件对象
方法filldata() 文件文件=新文件(路径);