我的异步任务有问题。每当我用后退按钮退出我的应用程序时,它就捕捉到一个异常。请看一下我的密码。请帮我纠正一下。谢谢。
androidfragment.java文件
public class AndroidFragment extends SherlockListFragment implements ActionBar.TabListener{
private ProgressDialog pDialog;
static final String URL = "https://dl.dropbox.com/s/86p8y5y2birua9n/test1.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
ArrayList<HashMap<String, String>> menuItems;
ListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
new loadListView().execute();
return super.onCreateView(inflater, container, savedInstanceState);
}
public class loadListView extends AsyncTask<Integer, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Integer... args) {
// updating UI from Background Thread
menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}
@Override
protected void onPostExecute(String args) {
adapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
setListAdapter(adapter);
//pDialog.dismiss();
}
}
@Override
public void onDestroy() {
if (adapter != null) {
setListAdapter(null);
}
super.onDestroy();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(android.R.id.content, this);
ft.attach(this);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.detach(this);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
LOGCAT:
10-06 00:17:55.750: E/AndroidRuntime(8780): FATAL EXCEPTION: main
10-06 00:17:55.750: E/AndroidRuntime(8780): java.lang.NullPointerException
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
10-06 00:17:55.750: E/AndroidRuntime(8780): at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment$loadListView.onPostExecute(AndroidFragment.java:96)
10-06 00:17:55.750: E/AndroidRuntime(8780): at in.wptrafficanalyzer.actionbarsherlocknavtab.AndroidFragment$loadListView.onPostExecute(AndroidFragment.java:1)
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.os.AsyncTask.finish(AsyncTask.java:602)
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.os.Looper.loop(Looper.java:137)
10-06 00:17:55.750: E/AndroidRuntime(8780): at android.app.ActivityThread.main(ActivityThread.java:4512)
10-06 00:17:55.750: E/AndroidRuntime(8780): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 00:17:55.750: E/AndroidRuntime(8780): at java.lang.reflect.Method.invoke(Method.java:511)
10-06 00:17:55.750: E/AndroidRuntime(8780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-06 00:17:55.750: E/AndroidRuntime(8780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-06 00:17:55.750: E/AndroidRuntime(8780): at dalvik.system.NativeStart.main(Native Method)
新LogCAT:
10-06 00:49:26.094: E/AndroidRuntime(10780): FATAL EXCEPTION: main
10-06 00:49:26.094: E/AndroidRuntime(10780): java.lang.NullPointerException
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
10-06 00:49:26.094: E/AndroidRuntime(10780): at in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment$loadListView.onPostExecute(AppleFragment.java:97)
10-06 00:49:26.094: E/AndroidRuntime(10780): at in.wptrafficanalyzer.actionbarsherlocknavtab.AppleFragment$loadListView.onPostExecute(AppleFragment.java:1)
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.os.AsyncTask.finish(AsyncTask.java:602)
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.os.Looper.loop(Looper.java:137)
10-06 00:49:26.094: E/AndroidRuntime(10780): at android.app.ActivityThread.main(ActivityThread.java:4512)
10-06 00:49:26.094: E/AndroidRuntime(10780): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 00:49:26.094: E/AndroidRuntime(10780): at java.lang.reflect.Method.invoke(Method.java:511)
10-06 00:49:26.094: E/AndroidRuntime(10780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
10-06 00:49:26.094: E/AndroidRuntime(10780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
10-06 00:49:26.094: E/AndroidRuntime(10780): at dalvik.system.NativeStart.main(Native Method)
最佳答案
我的猜测是,在创建简单适配器时,片段没有附加到活动。这是新的simpleadapter调用中唯一可以返回null的地方。将asynctask请求移动到onviewcreated并查看发生了什么。
更新:
我检查了simpleadapter文件中的源代码。引发错误的行正试图从传入的上下文(活动)创建LayoutFlater。这将返回null,因此getActivity()调用将返回null。
答:
如果没有附加片段,getActivity()将返回null,因此使用应用程序上下文创建SimpleAdapter。可以从传递给oncreateview的layoutinflater获取应用程序上下文。
关于android - Android AsyncTask退出异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12750306/