我正在读Android上的多线程。我没有发现我的错误。
我有一张显示位置的地图(从Android Map API v2演示应用程序复制)。我有一个AsyncTask我想从这个活动中调用但是一旦我调用它就会立即崩溃。 asynctask是一个httprequest封装。
首先我将myasynctask.execute()
放在onConnected(..)
函数上,但一旦活动开始(地图试图加载),应用就会崩溃。
@Override
public void onConnected(Bundle connectionHint) {
mLocationClient.requestLocationUpdates(
REQUEST,
this); // LocationListener
try{
mvba.execute(session.getUsername(),
String.valueOf(mLocationClient.getLastLocation().getLongitude()),
String.valueOf(mLocationClient.getLastLocation().getLatitude())
);
}catch(Exception e){
session.add_exception(e);
}
}
我也尝试将代码放在按钮的onClick功能上,它基本上适当地显示了我的地图和我的位置(所以我知道这是有效的)。然后当我点击按钮时,应用程序崩溃。
public void showMyLocation(View view) {
if (mLocationClient != null && mLocationClient.isConnected()) {
String msg = "Location = " + mLocationClient.getLastLocation();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
try{
mvba.execute(session.getUsername(),
String.valueOf(mLocationClient.getLastLocation().getLongitude()),
String.valueOf(mLocationClient.getLastLocation().getLatitude())
);
}catch(Exception e){
session.add_exception(e);
}
}
}
我没有logcat,因为我自己有一个带有kitkat的非root用户应用程序,在连接到任何PC时也遇到问题。是的,我知道logcat会有多么有用,而且我知道没有堆栈跟踪调试是多么困难。我知道它并且我正在协调一个解决方案,但此刻,我所拥有的只是我自己的手机。
更新1
我已尝试使用logcat访问应用程序来查看正在发生的事情,但这些需要带有root权限的手机或版本< 4.1。 我的AsyncTask基本上是我已经用作登录的另一个副本,并且有效。这是:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.content.Context;
import android.os.AsyncTask;
public class MapViewBackgroundActivity extends AsyncTask<String,Void,String>{
public AsyncResponse delegate = null;
private Context context;
//flag 0 means get and 1 means post.(By default it is get.)
public MapViewBackgroundActivity(Context context/*,TextView statusField,//
TextView roleField,int flag*/) {
this.context = context;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
String address ="http://www.pixeldonut.com/testing/tutorial";
try{
String username = (String)arg0[0];
String lastLong = (String)arg0[1];
String lastLat = (String)arg0[2];
address+="updateandget.php";
String data = URLEncoder.encode("username", "UTF-8")+ "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("lastLong", "UTF-8")+ "=" + URLEncoder.encode(lastLong, "UTF-8");
data += "&" + URLEncoder.encode("lastLat", "UTF-8")+ "=" + URLEncoder.encode(lastLat, "UTF-8");
URL url = new URL(address);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
delegate.processFinished(result);
}
}
更新2
这个人here似乎有相同的症状。我评论了
doInBackground(..)`的内容,但崩溃仍然发生。是不是因为这是我在整个应用程序生命周期中的第二次异步,所以我不知道发生了什么,因为我没有明确地关闭以前的asynctask。它是在另一个活动中,它成功地从服务器中检索信息(这是我的登录活动),所以我认为成功关闭并且可能不会成为这个新的asynctask的障碍。如果我可以添加,则通过.execute()进行调用。
更新3
我理解堆栈跟踪对此的帮助,如果我能拥有它,我就没有别的办法了。唉,我继续我的搜索,似乎正在磨练答案。
在MapViewActivity中,我调用MapViewBackgroundActivity,如下所示:
MapViewBackgroundActivity task= new MapViewBackgroundActivity(this);
public void showMyLocation(View view) {
if (mLocationClient != null && mLocationClient.isConnected()) {
String msg = "Location = " + mLocationClient.getLastLocation();
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
task.execute("",
"",
""
);
}
}
asynctask上的doInBacktround()
需要3个参数,这些参数曾经是实际值,但在我发布的代码中是我现在拥有它的方式。发生的事情是吐司消息出现然后停止2-3秒然后崩溃。如果我也注释掉doInBacktround()
的内容,那么崩溃仍然会发生,所以我知道错误是在这些代码行之间。
更新6
最后一些logcat。
02-04 23:21:45.936: I/GCoreUlr(1553): Received powerMode change from NLP: com.google.android.location.activity.LOW_POWER_MODE_DISABLED
02-04 23:21:45.936: I/GCoreUlr(1553): Starting service, intent=Intent { cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService (has extras) }, extras=Bundle[{intentType=3}]
02-04 23:21:45.996: I/GCoreUlr(1553): Ensuring that reporting is stopped because of reasons: {account#10#=[InactiveReason{mVersionCode=0, mIdentifier=10, mName='AuthError'}, InactiveReason{mVersionCode=0, mIdentifier=6, mName='ReportingNotSelected'}]}
02-04 23:21:45.996: D/GCoreFlp(1553): Unknown pending intent to remove.
02-04 23:21:45.996: I/GCoreUlr(1553): Unbound from all location providers
02-04 23:21:46.876: W/GAV2(27508): Thread[GAThread,5,main]: Service unavailable (code=1), will retry.
02-04 23:21:46.876: W/ActivityManager(509): Unable to start service Intent { act=com.google.android.gms.analytics.service.START cmp=com.google.android.gms/.analytics.service.AnalyticsService (has extras) } U=10: not found
02-04 23:21:46.886: I/GAV2(27508): Thread[GAThread,5,main]: No campaign data found.
02-04 23:21:47.476: I/ActivityManager(509): Killing 26675:com.google.android.deskclock/u0a12 (adj 15): empty #17
02-04 23:21:51.156: D/gpslogd(137): CALL_SENTRY: WriteData took 300 ms (from7813536 to 7813836) (log=0,0,0), start: 23:21:50.868
02-04 23:21:51.156: D/gpslogd(137): CALL_SENTRY: ProcessEvent took 300 ms (from7813536 to 7813836) (log=0,0,0), start: 23:21:50.868
02-04 23:21:51.186: W/GCoreFlp(1553): No location to return for getLastLocation()
02-04 23:21:51.226: I/Choreographer(27458): Skipped 44 frames! The application may be doing too much work on its main thread.
02-04 23:21:51.236: D/dalvikvm(1553): GC_CONCURRENT freed 285K, 5% free 8595K/9036K, paused 7ms+4ms, total 37ms
02-04 23:21:51.346: D/dalvikvm(1553): GC_CONCURRENT freed 441K, 6% free 8617K/9100K, paused 3ms+2ms, total 44ms
02-04 23:21:51.516: D/dalvikvm(27411): GC_CONCURRENT freed 621K, 7% free 10182K/10840K, paused 4ms+3ms, total 45ms
02-04 23:21:51.516: D/dalvikvm(27411): WAIT_FOR_CONCURRENT_GC blocked 31ms
02-04 23:21:51.886: W/GAV2(27508): Thread[Service Reconnect,5,main]: Service unavailable (code=1), using local store.
02-04 23:21:51.886: W/ActivityManager(509): Unable to start service Intent { act=com.google.android.gms.analytics.service.START cmp=com.google.android.gms/.analytics.service.AnalyticsService (has extras) } U=10: not found
02-04 23:22:00.156: D/dalvikvm(1553): GC_CONCURRENT freed 489K, 6% free 8634K/9164K, paused 2ms+3ms, total 35ms
02-04 23:22:00.156: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 13ms
02-04 23:22:05.196: D/dalvikvm(1553): GC_CONCURRENT freed 518K, 7% free 8627K/9184K, paused 4ms+2ms, total 38ms
02-04 23:22:05.196: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 12ms
02-04 23:22:10.166: D/dalvikvm(1553): GC_CONCURRENT freed 519K, 7% free 8619K/9184K, paused 3ms+2ms, total 27ms
02-04 23:22:10.166: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 20ms
02-04 23:22:15.186: D/dalvikvm(1553): GC_CONCURRENT freed 513K, 7% free 8617K/9184K, paused 2ms+2ms, total 25ms
02-04 23:22:15.186: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 11ms
02-04 23:22:16.396: D/dalvikvm(509): GC_EXPLICIT freed 893K, 15% free 18966K/22132K, paused 5ms+9ms, total 108ms
02-04 23:22:20.326: D/dalvikvm(1553): GC_CONCURRENT freed 473K, 6% free 8656K/9184K, paused 2ms+2ms, total 35ms
02-04 23:22:20.326: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 20ms
02-04 23:22:20.336: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 20ms
02-04 23:22:20.336: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 6ms
02-04 23:22:23.156: D/dalvikvm(1553): GC_CONCURRENT freed 518K, 8% free 8525K/9184K, paused 5ms+1ms, total 35ms
02-04 23:22:25.386: D/dalvikvm(1553): GC_CONCURRENT freed 398K, 6% free 8638K/9184K, paused 10ms+2ms, total 41ms
02-04 23:22:25.386: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 19ms
02-04 23:22:25.386: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 16ms
02-04 23:22:30.346: D/dalvikvm(1553): GC_CONCURRENT freed 520K, 7% free 8628K/9188K, paused 2ms+2ms, total 25ms
02-04 23:22:30.346: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 18ms
02-04 23:22:30.346: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 16ms
02-04 23:22:35.346: D/dalvikvm(1553): GC_CONCURRENT freed 523K, 7% free 8616K/9188K, paused 1ms+3ms, total 24ms
02-04 23:22:35.346: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 14ms
02-04 23:22:35.346: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 18ms
02-04 23:22:40.396: D/dalvikvm(1553): GC_CONCURRENT freed 519K, 7% free 8608K/9188K, paused 1ms+2ms, total 25ms
02-04 23:22:40.396: D/dalvikvm(1553): WAIT_FOR_CONCURRENT_GC blocked 10ms
02-04 23:22:41.376: W/GLSActivity(26807): [amr] Status from wire: NetworkError status: NETWORK_ERROR
02-04 23:22:41.376: I/GLSUser(26807): GLS error: NetworkError dr.martintaveras@gmail.com androidmarket
02-04 23:22:41.376: W/GLSActivity(26807): [amr] Status from wire: NetworkError status: NETWORK_ERROR
02-04 23:22:41.376: W/GLSActivity(1576): java.io.IOException: NetworkError
02-04 23:22:41.376: W/GLSActivity(1576): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
02-04 23:22:41.376: W/GLSActivity(1576): at com.google.android.gms.auth.GoogleAuthUtil.a(Unknown Source)
02-04 23:22:41.376: W/GLSActivity(1576): at com.google.android.gms.auth.GoogleAuthUtil.getTokenWithNotification(Unknown Source)
02-04 23:22:41.376: W/GLSActivity(1576): at com.google.android.gsf.loginservice.GoogleLoginService$AccountAuthenticatorImpl.getAuthToken(GoogleLoginService.java:296)
02-04 23:22:41.376: W/GLSActivity(1576): at android.accounts.AbstractAccountAuthenticator$Transport.getAuthToken(AbstractAccountAuthenticator.java:196)
02-04 23:22:41.376: W/GLSActivity(1576): at android.accounts.IAccountAuthenticator$Stub.onTransact(IAccountAuthenticator.java:113)
02-04 23:22:41.376: W/GLSActivity(1576): at android.os.Binder.execTransact(Binder.java:404)
02-04 23:22:41.376: W/GLSActivity(1576): at dalvik.system.NativeStart.run(Native Method)
02-04 23:22:41.376: E/PlayEventLogger(2030): Failed to get auth token: NetworkError
02-04 23:22:41.376: E/PlayEventLogger(2030): java.io.IOException: NetworkError
02-04 23:22:41.376: E/PlayEventLogger(2030): at android.accounts.AccountManager.convertErrorToException(AccountManager.java:1711)
02-04 23:22:41.376: E/PlayEventLogger(2030): at android.accounts.AccountManager.access$400(AccountManager.java:144)
02-04 23:22:41.376: E/PlayEventLogger(2030): at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:1572)
02-04 23:22:41.376: E/PlayEventLogger(2030): at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
02-04 23:22:41.376: E/PlayEventLogger(2030): at android.os.Binder.execTransact(Binder.java:404)
02-04 23:22:41.376: E/PlayEventLogger(2030): at dalvik.system.NativeStart.run(Native Method)
02-04 23:22:41.386: E/PlayEventLogger(2030): Upload failed class java.net.UnknownHostException(Unable to resolve host "play.googleapis.com": No address associated with hostname)
我的调试器堆栈跟踪
Thread [<1> main] (Suspended (exception NullPointerException))
<VM does not provide monitor information>
MapViewBackgroundActivity.onPostExecute(String) line: 58
MapViewBackgroundActivity.onPostExecute(Object) line: 1
MapViewBackgroundActivity(AsyncTask).finish(Object) line: 632
AsyncTask.access$600(AsyncTask, Object) line: 177
AsyncTask$InternalHandler.handleMessage(Message) line: 645
AsyncTask$InternalHandler(Handler).dispatchMessage(Message) line: 102
Looper.loop() line: 136
ActivityThread.main(String[]) line: 5017
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 515
ZygoteInit$MethodAndArgsCaller.run() line: 779
ZygoteInit.main(String[]) line: 595
NativeStart.main(String[]) line: not available [native method]
Thread [<10> Binder_2] (Running)
Thread [<9> Binder_1] (Running)
Thread [<19> Thread-966] (Running)
Thread [<18> GLThread 967] (Running)
Thread [<17> ibs] (Running)
Thread [<12> its] (Running)
Thread [<13> vts_traf_taveras.findme] (Running)
Thread [<16> vts_inaka_taveras.findme] (Running)
Thread [<11> vts_taveras.findme] (Running)
Thread [<15> vts_labl_taveras.findme] (Running)
Thread [<14> its_ter] (Running)
Thread [<20> RenderDrive] (Running)
Thread [<21> Binder_3] (Running)
Thread [<22> AsyncTask #1] (Running)
好的,我想我发现了错误。在logcat之后,相当明显的是什么,而不是为什么。在我最初的asynctask上,我使用了异步响应接口。在那个asynctask我需要做的就是按照规定here,返回主UI线程一些值。我评论说,现在一切都很好。
由于我已经学习了很多关于AsyncTask的知识,我可以将我的UI元素放到asynctask构造函数中并在OnPostExecute上编辑它们,而不是相反。我打算尝试一下,我想我今天已经完成了。
我感谢你的时间和任何线索,你可能会离开我。