我正在制作地图应用程序,我在两个点之间显示路线(动态)。在这里,我从数据库中检索纬度和经度(可以随时间变化)。这是一个大学项目,所以没有在安全部分工作。这是导航的java文件:
03-24 15:00:51.174 9970-9970/com.xyz.user.xyz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xyz.user.xyz, PID: 9970
java.lang.NullPointerException: PolylineOptions cannot be null.
at com.google.maps.api.android.lib6.common.k.a(:com.google.android.gms.DynamiteModulesB:42)
at com.google.maps.api.android.lib6.impl.dp.<init>(:com.google.android.gms.DynamiteModulesB:146)
at com.google.maps.api.android.lib6.impl.az.a(:com.google.android.gms.DynamiteModulesB:931)
at com.google.android.gms.maps.internal.l.onTransact(:com.google.android.gms.DynamiteModulesB:137)
at android.os.Binder.transact(Binder.java:380)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addPolyline(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
at com.xyz.user.xyz.NavigationActivity$ParserTask.onPostExecute(NavigationActivity.java:474)
at com.xyz.user.xyz.NavigationActivity$ParserTask.onPostExecute(NavigationActivity.java:420)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
这是我的logcat:
if(lineOptions!=null)
{
mMap.addPolyline(lineOptions);
}
有人能建议我一些解决方案吗?
我尝试使用以下代码:
mytrigfunction()
{
during this function execution
I compare 33032101292973583 from my sheet with current invoked trigger
if yes then send the mail to (example@it.com) person
otherwise
send mail to some other person (some@it.com) with some other trigger unique id matched
}
以上代码可以帮助应用程序避免崩溃,但它仍然没有帮助我在这些点之间绘制路径。
检索到的数据是正确的,我用吐司检查了它。
答案 0 :(得分:5)
我查看了你的代码并发现了崩溃原因的一些问题,首先你初始化了null ArrayList<LatLng>points=null
和PolylineOptions lineOptions=null
所以如果path为null然后lineOptions
太空,那么我修复下面的行审查代码应用它运行它,如果成功享受!
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = new ArrayList<LatLng>();;
PolylineOptions lineOptions = new PolylineOptions();;
lineOptions.width(2);
lineOptions.color(Color.RED);
MarkerOptions markerOptions = new MarkerOptions();
// Traversing through all the routes
for(int i=0;i<result.size();i++){
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
}
// Drawing polyline in the Google Map for the i-th route
if(points.size()!=0)mMap.addPolyline(lineOptions);//to avoid crash
}
答案 1 :(得分:0)
为什么要初始化ArrayList,在for循环下使用PolylineOption它会为循环运行创建新实例,所以尝试修复这些并添加两个mMap.moveCamera(CameraUpdateFactory.newLatLng(position)); mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions lineOptions = new PolylineOptions();;
MarkerOptions markerOptions = new MarkerOptions();
LatLng position = null;
// Traversing through all the routes
for(int i=0;i<result.size();i++){
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
lineOptions.addAll(points);
lineOptions.width(2);
lineOptions.color(Color.RED);
}
// Drawing polyline in the Google Map for the i-th route
mMap.addPolyline(lineOptions);
}
答案 2 :(得分:0)
只需在protected void onPostExecute(List&gt;&gt; result)方法中添加此行,即可避免崩溃
enter code here
if(points)!=null {
mMap.addPolyline(lineOptions);
}
但由于调用方向API时出错,因此返回null值为points。检查方向api是否正确启用
答案 3 :(得分:0)
尝试一下!
1。。无论如何,您都应在使用polyLineOptions
之前先对其进行检查,以查看其是否为空
if (polyLineOptions != null){
googleMap.addPolyline(polyLineOptions);
}
它将防止崩溃
2。。要绘制路线,请在getDirectionsUrl
的末尾添加API KEY,
字符串url =“ https://maps.googleapis.com/maps/api/directions/” +输出+“?” +参数+“&key =” +“您的密钥”;