我正在尝试为我的android studio移动应用获取2个位置之间的折线。但出现以下错误:试图在空对象引用上调用接口方法'int java.util.List.size()'
async_id
这是我的PointsParser.java
--------- 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)
这是我的Activity,调用PointsParse.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");
}
}
}
任何人都可以帮助我。谢谢
答案 0 :(得分:0)
如果看来super.logSomething()
方法中的结果参数是onPostExecute
。
通常,除非您知道返回的参数为NonNull,否则在从外部接收结果时请始终检查空指针。
null
这将防止启动器崩溃。
现在检查为什么结果返回为if (result != null) {
.... Your code ....
}
。
答案 1 :(得分:0)
在您的onPostExecute方法中进行更改:
<div class="table-div">
<table id="myTable">
<tr>
<th class="button"><button class="my-btn" type="button" onclick="sortTable()">
shuffle</button></th>
<th>Text:</th>
<th></th>
</tr>
<tr>
<td class="left">Some text 1</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 2</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 3</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 4</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 5</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 6</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 7</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
收件人:
ArrayList<LatLng> points;
答案 2 :(得分:0)
如日志中所示:
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)
问题在#63
行中:
for (int i = 0; i < result.size(); i++) {
因此,您的result
对象为null
,并且result.size()
引发异常。因此,可能的原因是在parser.parse(jObject)
调用中-恰好它填充了routes
对象的值。 DataParser
类没有源代码,但是如果您使用https://www.androidtutorialpoint.com中的代码,则意味着您使用的"results"
字符串中没有名为jsonData[0]
的数组进行解析。因此,在调用后打印url
:
String url=getUrl(place1.getPosition(), place2.getPosition(),"driving");
通过致电:
Log.d("mylog", "url='" + url + "'");
然后将其复制到浏览器地址行,执行并分析服务器响应:如果没有带有"results"
标签的数组,则应检查key
和其他请求参数。