我正在尝试将用户的Facebook个人资料照片应用为标记中的“图标”属性。但由于某些未知原因,我这样做时会收到Nullpointer异常。
语法是正确的,我只是切断了一些代码,只是为了向你展示影响它的唯一一点(与标记有关)。
AccessToken accessToken = AccessToken.getCurrentAccessToken();
MarkerOptions marker = new MarkerOptions().position(
new LatLng(point.latitude, point.longitude)).title("New Marker");
globalMap.addMarker(marker
.position(point)
.icon(BitmapDescriptorFactory.fromBitmap(getFacebookProfilePicture(accessToken.getUserId())))
.anchor(0.5f, 1));
}
});
}
public static Bitmap getFacebookProfilePicture(String userID){
Bitmap bitmap = null;
try {
URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
}catch(Exception e){
}
return bitmap;
}
我认为正是这条线给了我这个问题,但是......
icon(BitmapDescriptorFactory.fromBitmap(getFacebookProfilePicture(accessToken.getUserId())))
......这就是logcat所说的:
java.lang.NullPointerException
at maps.f.g.a(Unknown Source)
at maps.ag.g$a.<init>(Unknown Source)
at maps.ag.g.a(Unknown Source)
at maps.ag.R.<init>(Unknown Source)
at maps.ag.t.a(Unknown Source)
答案 0 :(得分:1)
您的错误在于您没有建立实际的网络连接。在加载用户的个人资料图像之前,您应该调用URLConnection.Connect()方法。
try {
URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
connection.setDoInput(true);
connection.connect();
bitmap = BitmapFactory.decodeStream(connection.getInputStream());
catch(IOException ex) {
}