我对 AsyncTask 和 ProgressDialog 有点陌生,每当我调用new MyTask().execute();
按钮时,我都会收到空指针异常错误,我的方法不正确吗?我的代码有什么问题或我缺少什么?
只有它应该处理的过程取决于当前TabHost
选择的所以..
这是我的代码:
public class MyTask extends AsyncTask<Void, Integer, Void>{
@Override
protected void onPreExecute() {
//Show progress Dialog here
super.onPreExecute();
progress.show();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//Update UI here if needed
//Dismiss Progress Dialog here
progress.dismiss();
}
@Override
protected Void doInBackground(Void... params) {
SparseBooleanArray laptopBooleanArray = laptopLV.getCheckedItemPositions();
SparseBooleanArray mp3BooleanArray = mp3LV.getCheckedItemPositions();
SparseBooleanArray mobilepBooleanArray = mobileLV.getCheckedItemPositions();
switch (th.getCurrentTab()) {
case 0:
for(int i = 0; i < laptopLV.getCount(); i++)
{
if(laptopBooleanArray.get(i) == true)
{
selectedFromLaptopList.add(laptopLV.getItemAtPosition(i).toString());
}
}
handler.setItemListData(TechiesActivity.this, "Techies", selectedFromLaptopList, "Unit" , "Laptop" , "Store");
displayView();
drawer.closeMenu();
selectedFromLaptopList.clear();
break;
case 1:
for(int i = 0; i < mp3LV.getCount(); i++)
{
if(mp3BooleanArray.get(i) == true)
{
selectedFromLaptopList.add(mp3LV.getItemAtPosition(i).toString());
}
}
handler.setItemListData(TechiesActivity.this, "Techies", selectedFromMp3List, "Unit" , "Mp3" , "Store");
displayView();
drawer.closeMenu();
selectedFromLaptopList.clear();
break;
case 2:
for(int i = 0; i < mobileLV.getCount(); i++)
{
if(mobilepBooleanArray.get(i) == true)
{
selectedFromLaptopList.add(mobileLV.getItemAtPosition(i).toString());
}
}
handler.setItemListData(TechiesActivity.this, "Techies", selectedFromMobileList, "Unit" , "Mobile" , "Store");
displayView();
drawer.closeMenu();
selectedFromLaptopList.clear();
break;
}
return null;
}
}
这是 LogCat 错误:
03-05 13:15:05.650: E/AndroidRuntime(497): FATAL EXCEPTION: AsyncTask #1
03-05 13:15:05.650: E/AndroidRuntime(497): java.lang.RuntimeException: An error occured while executing doInBackground()
03-05 13:15:05.650: E/AndroidRuntime(497): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.lang.Thread.run(Thread.java:1096)
03-05 13:15:05.650: E/AndroidRuntime(497): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-05 13:15:05.650: E/AndroidRuntime(497): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.view.View.invalidate(View.java:5139)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.widget.AbsListView.resetList(AbsListView.java:1011)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.widget.ListView.resetList(ListView.java:493)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.widget.ListView.setAdapter(ListView.java:422)
03-05 13:15:05.650: E/AndroidRuntime(497): at com.shop.browser.TechiesActivity.displayView(TechiesActivity.java:98)
03-05 13:15:05.650: E/AndroidRuntime(497): at com.shop.browser.TechiesActivity$MyTask.doInBackground(TechiesActivity.java:257)
03-05 13:15:05.650: E/AndroidRuntime(497): at com.shop.browser.TechiesActivity$MyTask.doInBackground(TechiesActivity.java:1)
03-05 13:15:05.650: E/AndroidRuntime(497): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-05 13:15:05.650: E/AndroidRuntime(497): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-05 13:15:05.650: E/AndroidRuntime(497): ... 4 more
03-05 13:15:07.850: E/WindowManager(497): Activity com.shop.browser.TechiesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f93528 that was originally added here
03-05 13:15:07.850: E/WindowManager(497): android.view.WindowLeaked: Activity com.shop.browser.TechiesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f93528 that was originally added here
03-05 13:15:07.850: E/WindowManager(497): at android.view.ViewRoot.<init>(ViewRoot.java:247)
03-05 13:15:07.850: E/WindowManager(497): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-05 13:15:07.850: E/WindowManager(497): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-05 13:15:07.850: E/WindowManager(497): at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-05 13:15:07.850: E/WindowManager(497): at android.app.Dialog.show(Dialog.java:241)
03-05 13:15:07.850: E/WindowManager(497): at com.shop.browser.TechiesActivity$MyTask.onPreExecute(TechiesActivity.java:233)
03-05 13:15:07.850: E/WindowManager(497): at android.os.AsyncTask.execute(AsyncTask.java:391)
03-05 13:15:07.850: E/WindowManager(497): at com.shop.browser.TechiesActivity.onClick(TechiesActivity.java:181)
03-05 13:15:07.850: E/WindowManager(497): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 13:15:07.850: E/WindowManager(497): at java.lang.reflect.Method.invoke(Method.java:521)
03-05 13:15:07.850: E/WindowManager(497): at android.view.View$1.onClick(View.java:2067)
03-05 13:15:07.850: E/WindowManager(497): at android.view.View.performClick(View.java:2408)
03-05 13:15:07.850: E/WindowManager(497): at android.view.View$PerformClick.run(View.java:8816)
03-05 13:15:07.850: E/WindowManager(497): at android.os.Handler.handleCallback(Handler.java:587)
03-05 13:15:07.850: E/WindowManager(497): at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 13:15:07.850: E/WindowManager(497): at android.os.Looper.loop(Looper.java:123)
03-05 13:15:07.850: E/WindowManager(497): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-05 13:15:07.850: E/WindowManager(497): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 13:15:07.850: E/WindowManager(497): at java.lang.reflect.Method.invoke(Method.java:521)
03-05 13:15:07.850: E/WindowManager(497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-05 13:15:07.850: E/WindowManager(497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-05 13:15:07.850: E/WindowManager(497): at dalvik.system.NativeStart.main(Native Method)
根据要求的按钮代码:
public void onClick (View v) {
new MyTask().execute();
}
任何意见或答案将不胜感激。