I'm reading up on multithreading on Android. I haven't found my error.
I have a map (copied from Android Map API v2 Demo app) which displays location. I have an AsyncTask I want to call from this activity but once i call it it immediately crashes. The asynctask is an httprequest encapsulation.
First I placed myasynctask.execute()
on the onConnected(..)
function, but once the activity got started (the map tried to load) the app would crash.
@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);
}
}
I also tried placing the code on the onClick function of the button, which basically displays my map and my location appropriately (so i know that is working). Then when I click the button the app crashes.
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);
}
}
}
I do not have logcat as I myself have a non-rooted app with kitkat, that also experiences problems while connecting to any PC. Yes I know how useful logcat would be, and I know how difficult it is to debug without a stack trace. I am aware of it and I'm coordinating a solution but at the moment, all I have is my own phone.
UPDATE 1
I've tried logcat-accessing apps to see what's going on but these require a rooted phone or version < 4.1. My AsyncTask is basically a copy of another that I'm already using as a log in, and that works. Here it is:
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);
}
}
UPDATE 2
It seems this person here has the same symptoms. I commented out content of
doInBackground(..)` and the crash still occurs. Is it possibly that since this is my second asynctask throughout the app lifecycle, something unknown to me occurs because I don't explicitly close the previous asynctask. It is in another activity and it retreives information from the server succesfully (this is my login activity), so I think that closes successfuly and probably isnt a hindrance to this new asynctask. All being called through .execute() if i may add.
UPDATE 3
I understand the stack trace's aid in this, if I could have it, I would have it no other way. Alas I continue on my search and seem to be honing in on the answer.
In MapViewActivity I call MapViewBackgroundActivity, like so:
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("",
"",
""
);
}
}
The doInBacktround()
on the asynctask expects 3 parameters, these used to be the actual values but in my posted code is how I have it right now. What happens is the toast message appears and then stalls for 2-3 seconds and then crashes. If I also comment out the content of doInBacktround()
the crash still happens, so I know the error is between these lines of code.
UPDATE 6
Finally some 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)
Also my debugger stack trace
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)
Ok i think i found the error. After logcat it's fairly obvious what, not why. On my initial asynctask i used an asyncresponse interface. In that asynctask all i was required to do was as prescribed here, to return to the main UI thread some value. I commented that out and everything looks fine for now.
And since I've learned so much about AsyncTask, I can take my UI elements into the asynctask constructor and edit them on OnPostExecute, instead of the other way around. I'm going to try that and i think im done for today.
I appreciate your time and any clue you may leave me.