我有一个导航抽屉和一些异步任务,它将在后台执行。我想要做的是当从导航抽屉中选择某个项目时,我然后执行在异步任务中在后台执行的方法。以下是从导航抽屉中选择内容的代码:
private void selectItem(int position) {
selectedPosition = position;
mDrawerLayout.closeDrawer(navDrawerView);
switch (selectedPosition) {
case 0:
try {
ViewEventsOnMap();
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(ENeighbourhoodActivity.this, "Press and hold at the area where you wish to create an event!",
Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(ENeighbourhoodActivity.this, "Analyze Event",
Toast.LENGTH_LONG).show();
break;
}
setTitle(mEventSelection[selectedPosition]);
}
我的异步任务类:
private class MyAsyncTask extends AsyncTask<Void, Integer, Double> {
@Override
protected Double doInBackground(Void... params) {
try {
ViewEventsOnMap();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Double result) {
}
protected void onProgressUpdate(Integer... progress) {
}
}
我在onCreate时调用了这个异步任务类,因此它不依赖于从导航抽屉中选择的项目。
public void ViewEventsOnMap() throws JSONException {
String page;
JSONArray jsonArray;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(URL);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
try {
// Codes to retrieve data from servlet and plot marker onto the map
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
但是使用这些代码,我收到以下错误消息:
11-04 20:54:45.642: W/dalvikvm(25527): threadid=1: thread exiting with uncaught exception (group=0x40c3f1f8)
11-04 20:54:45.673: E/AndroidRuntime(25527): FATAL EXCEPTION: main
11-04 20:54:45.673: E/AndroidRuntime(25527): android.os.NetworkOnMainThreadException
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11-04 20:54:45.673: E/AndroidRuntime(25527): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-04 20:54:45.673: E/AndroidRuntime(25527): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-04 20:54:45.673: E/AndroidRuntime(25527): at libcore.io.IoBridge.connect(IoBridge.java:112)
11-04 20:54:45.673: E/AndroidRuntime(25527): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-04 20:54:45.673: E/AndroidRuntime(25527): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-04 20:54:45.673: E/AndroidRuntime(25527): at java.net.Socket.connect(Socket.java:842)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-04 20:54:45.673: E/AndroidRuntime(25527): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-04 20:54:45.673: E/AndroidRuntime(25527): at nyp.edu.eneighbourhood.ENeighbourhoodActivity.ViewEventsOnMap(ENeighbourhoodActivity.java:204)
11-04 20:54:45.673: E/AndroidRuntime(25527): at nyp.edu.eneighbourhood.ENeighbourhoodActivity.selectItem(ENeighbourhoodActivity.java:385)
11-04 20:54:45.673: E/AndroidRuntime(25527): at nyp.edu.eneighbourhood.ENeighbourhoodActivity.access$4(ENeighbourhoodActivity.java:378)
11-04 20:54:45.673: E/AndroidRuntime(25527): at nyp.edu.eneighbourhood.ENeighbourhoodActivity$5.onChildClick(ENeighbourhoodActivity.java:341)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:584)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:521)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2713)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.widget.AbsListView$1.run(AbsListView.java:3468)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.os.Handler.handleCallback(Handler.java:605)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.os.Handler.dispatchMessage(Handler.java:92)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.os.Looper.loop(Looper.java:137)
11-04 20:54:45.673: E/AndroidRuntime(25527): at android.app.ActivityThread.main(ActivityThread.java:4512)
11-04 20:54:45.673: E/AndroidRuntime(25527): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 20:54:45.673: E/AndroidRuntime(25527): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 20:54:45.673: E/AndroidRuntime(25527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
11-04 20:54:45.673: E/AndroidRuntime(25527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
11-04 20:54:45.673: E/AndroidRuntime(25527): at dalvik.system.NativeStart.main(Native Method)
任何指南?提前谢谢。
答案 0 :(得分:2)
你写了一个AsyncTask
课程,但你没有使用它。您的堆栈跟踪显示您在主应用程序线程上从ViewEventsOnMap()
调用了selectItem()
,并且您没有在任何地方使用MyAsyncTask
。
答案 1 :(得分:0)
您没有使用AsyncTask
进行网络通话。您已撰写AsyncTask
但未完全使用它。
在导航抽屉中选择项目时,您将直接调用在UI线程上运行的方法ViewEventsOnMap()
。
而不是这样做,你应该调用你的AsycnTask