gpt4 book ai didi

java - Android Studio错误: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference

转载 作者:行者123 更新时间:2023-12-02 00:44:48 27 4
gpt4 key购买 nike

我正在尝试为我的 Android Studio 移动应用程序获取两个位置之间的折线。但我收到以下错误:尝试在空对象引用上调用接口(interface)方法“int java.util.List.size()”

    --------- beginning of crash
2019-09-12 21:14:14.297 5106-5106/com.hfad.los E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hfad.los, PID: 5106
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.hfad.los.PointsParser.onPostExecute(PointsParser.java:63)
at com.hfad.los.PointsParser.onPostExecute(PointsParser.java:23)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

这是我的 PointsParser.java

package com.hfad.los;

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.util.Log;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import com.hfad.los.DataParser;
import com.hfad.los.TaskLoadedCallback;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* Created by Vishal on 10/20/2018.
*/

public class PointsParser extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
TaskLoadedCallback taskCallback;
String directionMode = "driving";

public PointsParser(Context mContext, String directionMode) {
this.taskCallback = (TaskLoadedCallback) mContext;
this.directionMode = directionMode;
}

// Parsing the data in non-ui thread
@Override
protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;

try {
jObject = new JSONObject(jsonData[0]);
Log.d("mylog", jsonData[0].toString());
DataParser parser = new DataParser();
Log.d("mylog", parser.toString());

// Starts parsing data
routes = parser.parse(jObject);
Log.d("mylog", "Executing routes");
Log.d("mylog", routes.toString());

} catch (Exception e) {
Log.d("mylog", e.toString());
e.printStackTrace();
}
return routes;
}

// Executes in UI thread, after the parsing process
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points;
PolylineOptions lineOptions = null;
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<>();
lineOptions = new PolylineOptions();
// 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);
if (directionMode.equalsIgnoreCase("walking")) {
lineOptions.width(10);
lineOptions.color(Color.MAGENTA);
} else {
lineOptions.width(20);
lineOptions.color(Color.BLUE);
}
Log.d("mylog", "onPostExecute lineoptions decoded");
}

// Drawing polyline in the Google Map for the i-th route
if (lineOptions != null) {
//mMap.addPolyline(lineOptions);
taskCallback.onTaskDone(lineOptions);

} else {
Log.d("mylog", "without Polylines drawn");
}
}
}

这是我调用 PointsParse.java 的 Activity

package com.hfad.los;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

public class TrackOrderCust extends AppCompatActivity implements OnMapReadyCallback, TaskLoadedCallback {

GoogleMap map;
Button track;
MarkerOptions place1, place2;
Polyline currentPolyline;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track_order_cust);
track=(Button) findViewById(R.id.track);

MapFragment mapFragment=(MapFragment) getFragmentManager().findFragmentById(R.id.mapFrag);

mapFragment.getMapAsync(this);
place1=new MarkerOptions().position(new LatLng(2.9198401,101.780868)).title("You");
place2=new MarkerOptions().position(new LatLng(2.9785683,101.736)).title("Vendor");

String url=getUrl(place1.getPosition(), place2.getPosition(),"driving");
new FetchURL(TrackOrderCust.this).execute(url,"driving");






}

@Override
public void onMapReady(GoogleMap googleMap) {
map=googleMap;
map.addMarker(place1);
map.addMarker(place2);


}

private String getUrl(LatLng origin, LatLng dest, String directionMode){
String str_origin="origin:"+origin.latitude+","+origin.longitude;
String str_dest="destination:"+dest.latitude+","+dest.longitude;
String mode="mode:" + directionMode;
String parameters = str_origin + "&" + str_dest + "&" + mode;
String output = "json";

String url = "https://maps.googleapis.com/maps/api/directions/" + output +"?"+parameters+"&key="+getString(R.string.google_maps_key);
return url;
}

@Override
public void onTaskDone(Object... values) {
if(currentPolyline!=null)
currentPolyline.remove();
currentPolyline=map.addPolyline((PolylineOptions) values[0]);
}
}

谁能帮帮我。谢谢

最佳答案

在 onPostExecute 方法中更改:

ArrayList<LatLng> points;

至:

ArrayList<LatLng> points = new ArrayList<>();

关于java - Android Studio错误: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57907892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com