Problems while trying to see the route between two locations using Google Maps
I'm using a background thread to read from a Sqlite database some GPS data...and I store it in some GeoPoints.....After I finish reading the data I wanna use an intent in order to see the route described by that data.
Here is my code:
protected List<GeoPoint> doInBackground(DBAdapter... db) {
try {
db[0].openDataBase();
Cursor c = db[0].getAllData();
if (c.moveToFirst()) {
do {
longitude = Integer.parseInt(c.getString(0));
latitude = Integer.parseInt(c.getString(1));
p = new GeoPoint(latitude, longitude);
geoPointsArray.add(p);
} while (c.moveToNext());
}
c.close();
db[0].close();
} catch (Exception e) {
Log.d("Eroare", "doInBackground", e);
}
return geoPointsArray;
}
protected void onPostExecute(List<GeoPoint> geoPointsArray) {
int i = geoPointsArray.size();
srcPlace = geoPointsArray.get(1);
destPlace = geoPointsArray.get(i);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(" http://maps.google.com?t=k&saddr=" + srcPlace
+ "&daddr=" + destPlace));
startActivity(intent);
}
But something, somewhere goes terribly wrong and I get the next output of the logcat:
Uncaught handler: thread main exiting due to uncaught exception
java.lang.IndexOutOfBoundsException: Invalid location 4180, size is 4180
at java.util.ArrayList.get(ArrayList.java:341)
at test.android.screen_database$InitTask.onPostExecute(screen_database.java:201)
at test.android.screen_database$InitTask.onPostExecute(screen_database.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Don't forget I wanna see the path on the map between those two points...so if u have suggestions about the URL are more than welcomed!
UPDATE:This is my second FC
thread exiting with uncaught exception (group=0x4001b188)
Uncaught handler: thread main exiting due to uncaught exception
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat= http://maps.google.com?t=k&saddr=48856838,2351068&daddr=48927049,2357270 } at android.app.I开发者_运维百科nstrumentation.checkStartActivityResult(Instrumentation.java:1408)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
at android.app.Activity.startActivityForResult(Activity.java:2749)
at android.app.Activity.startActivity(Activity.java:2855)
at test.android.screen_database$InitTask.onPostExecute(screen_database.java:205)
at test.android.screen_database$InitTask.onPostExecute(screen_database.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
The array index is based on zero. If i
is your size, you need to call destPlace = geoPointsArray.get(i - 1);
. And the source should be 0
and not 1
.
protected void onPostExecute(List<GeoPoint> geoPointsArray) {
int i = geoPointsArray.size();
srcPlace = geoPointsArray.get(0);
destPlace = geoPointsArray.get(i - 1);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(" http://maps.google.com?t=k&saddr=" + srcPlace
+ "&daddr=" + destPlace));
startActivity(intent);
}
Your error is in
int i = geoPointsArray.size();
srcPlace = geoPointsArray.get(1);
destPlace = geoPointsArray.get(i);
needs to be destPlace = geoPointsArray.get(i-1);
精彩评论