Java 无法添加到我的列表,引发NullPointerException
我想我想做的事情相当简单,但我来自PHP背景(你知道,大部分繁重的工作都是为你完成的) 基本上,我的日志如下:Java 无法添加到我的列表,引发NullPointerException,java,android,Java,Android,我想我想做的事情相当简单,但我来自PHP背景(你知道,大部分繁重的工作都是为你完成的) 基本上,我的日志如下: 12-06 15:45:18.282: D/MAPVIEW(32047): Matching Providers are: [network, gps] 12-06 15:45:18.282: D/MAPVIEW(32047): Um, whats the best provder? Well its obviously 'network' 12-06 15:45:18.753: D/
12-06 15:45:18.282: D/MAPVIEW(32047): Matching Providers are: [network, gps]
12-06 15:45:18.282: D/MAPVIEW(32047): Um, whats the best provder? Well its obviously 'network'
12-06 15:45:18.753: D/MAPVIEW(32047): Location Data: Lat:49, Long: -116
12-06 15:45:18.753: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.753: D/MAPVIEW(32047): Location Data: Lat:50, Long: -116
12-06 15:45:18.753: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.753: D/MAPVIEW(32047): Location Data: Lat:49, Long: -117
12-06 15:45:18.753: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.753: D/MAPVIEW(32047): Location Data: Lat:49, Long: -115
12-06 15:45:18.753: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.753: D/MAPVIEW(32047): Location Data: Lat:49, Long: -117
12-06 15:45:18.753: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.753: D/MAPVIEW(32047): Location Data: Lat:49, Long: -117
12-06 15:45:18.763: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.763: D/MAPVIEW(32047): Location Data: Lat:50, Long: -115
12-06 15:45:18.763: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.763: D/MAPVIEW(32047): Location Data: Lat:49, Long: -116
12-06 15:45:18.763: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.763: D/MAPVIEW(32047): Location Data: Lat:50, Long: -118
12-06 15:45:18.763: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.763: D/MAPVIEW(32047): Location Data: Lat:49, Long: -114
12-06 15:45:18.783: D/MAPVIEW(32047): Could not add coordinate array to List, Error: java.lang.NullPointerException
12-06 15:45:18.783: D/AndroidRuntime(32047): Shutting down VM
12-06 15:45:18.783: W/dalvikvm(32047): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
您将看到我正在尝试添加到坐标列表中,而try-catch正在捕获异常。
(暂时不要担心撞车,我会处理的。)
因此,没有更多的adeu,以下是我的相关代码:
package com.private.clientndablocked;
// ... imports
public class ShowMapView extends MapActivity {
// ... some initializers
List<int[]> allLocations;
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// .. whole bunch of other unreleated stuff
// A single json object, this gets populated with cherry picked json array items
JSONObject obj = null;
// go through each of the items in the json array
for(int i = 0; i < json.length(); i++) {
try {
// We must try to get the object first
obj = (JSONObject) json.get(i);
} catch (JSONException e) {
e.printStackTrace();
}
// Initialize temp data with defaults
int Latitude = 0;
int Longitude = 0;
String Title = "";
String ID = "";
int[] coords = new int[2];
// So we actually have a picked item?
if(obj != null)
try {
// Lets try to assign the temp vars with their real data
Latitude = (int) (obj.getDouble("lat") * 1e6);
Longitude = (int) (obj.getDouble("long") * 1e6);
Title = (String) (obj.getString("title"));
ID = (String) (obj.getString("id"));
// Save the lat and long of the new location data (non micro)
// This stuff goes into allLocations List
coords[0] = obj.getInt("lat");
coords[1] = obj.getInt("long");
} catch (JSONException e) {
e.printStackTrace();
}
// Create a new GeoPoint to store the microdegrees into
GeoPoint point = new GeoPoint(
Latitude,
Longitude
);
// Create a new OverlayItem with the GeoPoint and a title and snippet
OverlayItem overlayitem = new OverlayItem(
point,
Title,
ID
);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Log.d("MAPVIEW", "Location Data: Lat:" + coords[0] + ", Long: " + coords[1] );
try{
allLocations.add(coords); // ************* this is whats failing ******************
} catch(Exception e) {
Log.d("MAPVIEW", "Could not add coordinate array to List, Error: " + e.toString());
}
}
// ... whole bunch more unrelated stuff
}
上面是大for循环json提取器。这将使程序抛出UnsupportedOperationException
老实说,我不知道还能做什么
我以为我已经弄明白了,但显然我没有:(
(注:我想像这样存储这些,这样我就可以做到:
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
for (int[] l : allLocations) {
int lat = (int) l[0];
int lon = (int) l[1];
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
}
我看不到您已初始化列表的位置。您必须先初始化列表才能使用它
allLocations = new ArrayList<int[]>();
未获得编译异常。该方法采用array参数。请检查:我看不出您在哪里初始化了列表。必须先初始化列表才能使用它
allLocations = new ArrayList<int[]>();
没有得到编译异常。该方法采用array参数。请检查:AH!!我们开始了!看,我试图这么做,但由于我是一个新手,我一定是在什么地方弄乱了语法,因为Eclipse..在我键入最终文本时创建了整个类重写方法)字符,但仍然出错,所以我想我走错了路:(感谢您的超快速响应!感谢上帝。我刚刚花了一个小时制作和制作了其他非必要的东西。Php让事情变得如此简单,但我必须承认,java变得非常棒。啊!!我们开始了!看,我试着这么做,但作为一个新手,我一定是在什么地方弄乱了语法,因为Eclipse..创建了整个当我输入最后一个)字符时,类重写方法仍然出错,所以我想我走错了路:(感谢您的超快速响应!感谢上帝。我只花了一个小时来铸造和做其他非必要的东西。Php让事情变得如此简单,但我必须承认,java变得非常棒。
Arrays.asList