我们开发了一款可在Google Play上使用的应用,但它有一些小错误。
你知道,有些设备报告我们的应用程序在全新安装时崩溃。
崩溃报告如下:
java.lang.NullPointerException:at com.atry.kil0b1te.sample.MapsActivity2.build_retrofit_and_get_response (MapsActivity2.java:259)at com.atry.kil0b1te.sample.MapsActivity2.access $ 100 (MapsActivity2.java:71)at com.atry.kil0b1te.sample.MapsActivity2 $ 2.run(MapsActivity2.java:163) 在android.os.Handler.handleCallback(Handler.java:761)at android.os.Handler.dispatchMessage(Handler.java:98)at android.os.Looper.loop(Looper.java:156)at android.app.ActivityThread.main(ActivityThread.java:6605)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:999)com.android.internal.os.ZygoteInit.main (ZygoteInit.java:889)
IT指的是这种方法 build_retrofit_and_get_response方法:
private void build_retrofit_and_get_response(String type) {
String url = "https://maps.googleapis.com/maps/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitMaps service = retrofit.create(RetrofitMaps.class);
Call<Example> call = service.getDistanceDuration("metric", origin.latitude + "," + origin.longitude,dest.latitude + "," + dest.longitude, type);
//showCoordinatesInfo();
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Response<Example> response, Retrofit retrofit) {
try {
//Remove previous line from map
if (line != null) {
line.remove();
}
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getRoutes().size(); i++) {
String distance = response.body().getRoutes().get(i).getLegs().get(i).getDistance().getText();
String time = response.body().getRoutes().get(i).getLegs().get(i).getDuration().getText();
ShowDistanceDuration.setText("Distance: " + distance + "\nDuration: " + time);
String encodedString = response.body().getRoutes().get(0).getOverviewPolyline().getPoints();
List<LatLng> list = decodePoly(encodedString);
line = mMap.addPolyline(new PolylineOptions()
.addAll(list)
.width(10)
.color(Color.RED)
.geodesic(true)
);
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
switch(type){
case "driving":
btnDriving.setTextColor(getResources().getColor(R.color.active_text));
btnWalk.setTextColor(getResources().getColor(R.color.black));
break;
case "walking":
btnDriving.setTextColor(getResources().getColor(R.color.black));
btnWalk.setTextColor(getResources().getColor(R.color.active_text));
break;
}
}
具体而言,第259行是:
Call<Example> call = service.getDistanceDuration("metric", origin.latitude + "," + origin.longitude,dest.latitude + "," + dest.longitude, type);
原点在此方法中初始化:
@Override
public void onLocationChanged(Location location) {
origin = new LatLng(location.getLatitude(), location.getLongitude());
float[] distance = new float[2];
Location.distanceBetween(origin.latitude, origin.longitude, circle.getCenter().latitude,circle.getCenter().longitude,distance);
if (distance[0] <= circle.getRadius() && !arrived)
{
arrived = true;
markAsVisited();
sendNotif();
arrivalAlert();
}
switch (travel_mode){
case "driving":
build_retrofit_and_get_response("driving");
break;
case "walking":
build_retrofit_and_get_response("walking");
break;
}
}
这只发生在全新安装上!您对如何解决这次崩溃有任何想法吗?谢谢。
答案 0 :(得分:0)
我自己修理了应用程序lol
问题是由我的Java类中的这行代码引起的:
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
所以我将该代码移动到应用程序的启动。现在崩溃是固定的。请查看我们的应用here。