我想在地图上显示一个点作为我的位置,但是当我在地图上设置相机时,为什么会出现此错误?
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chinatown.wangjian.chinatown_nearbyme/com.chinatown.wangjian.chinatown_nearbyme.MainActivity}: java.lang.NullPointerException: CameraUpdateFactory is not initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
at android.app.ActivityThread.access$600(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4796)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:543)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized
at com.google.android.gms.common.internal.zzx.zzb(Unknown Source)
at com.google.android.gms.maps.CameraUpdateFactory.zzqf(Unknown Source)
at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source)
at com.chinatown.wangjian.chinatown_nearbyme.MainActivity.onCreate(MainActivity.java:71)
at android.app.Activity.performCreate(Activity.java:5031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
关于CameraUpdateFactory的完整代码如下所示,这有什么问题吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_activity);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading Nearest Restaurant...");
pDialog.show();
gps = new GPSTracker(this);
textStatus = (TextView)findViewById(R.id.textStatus);
listView = (ListView)findViewById(R.id.resturant_list);
mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.restaurant_map)).getMap();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15));
gp = new GooglePlaceSearch(ApiKey);
gp.setOnPlaceResponseListener(new OnPlaceResponseListener() {
public void onResponse(String status, ArrayList<ContentValues> arr_data, Document doc) {
Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();
textStatus.setText("Search Restaurant Nearby : " + status);
pDialog.dismiss();
我在这个答案上发现了好几次,但如果我的代码有问题,总是会失败?请帮助Thx。
地图的更多详细信息
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15));
gp = new GooglePlaceSearch(ApiKey);
gp.setOnPlaceResponseListener(new OnPlaceResponseListener() {
public void onResponse(String status, ArrayList<ContentValues> arr_data, Document doc) {
Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();
textStatus.setText("Search Restaurant Nearby : " + status);
pDialog.dismiss();
if(status.equals(GooglePlaceSearch.STATUS_OK)) {
ArrayList<String> array = new ArrayList<String>();
final ArrayList<String> array_photo = new ArrayList<String>();
for(int i = 0 ; i < arr_data.size() ; i++) {
String title = arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME);
double lat = arr_data.get(i).getAsDouble(GooglePlaceSearch.PLACE_LATITUDE);
double lng = arr_data.get(i).getAsDouble(GooglePlaceSearch.PLACE_LONGITUDE);
LatLng pos = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(pos).title(title));
array.add("Name : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_NAME) + "\n"
+ "Address : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_ADDRESS) + "\n"
+ "Latitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LATITUDE) + "\n"
+ "Longitude : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_LONGITUDE) + "\n"
+ "Phone Number : " + arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHONENUMBER));
array_photo.add(arr_data.get(i).getAsString(GooglePlaceSearch.PLACE_PHOTO));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this
, R.layout.restaurant_listview_text, array);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.restaurant_dialog_photo);
dialog.setCancelable(true);
final ImageView imgPhoto = (ImageView)dialog.findViewById(R.id.imgPhoto);
dialog.show();
gp.getPhotoBitmapByWidth(array_photo.get(arg2), 600, ""
, new OnBitmapResponseListener() {
public void onResponse(Bitmap bm, String tag) {
imgPhoto.setImageBitmap(bm);
}
});
}
});
}
}
});
gp.getNearby(gps.getLatitude(), gps.getLongitude(), radius, type, language);
//gp.getRadarSearch(latitude, longitude, radius, type, language, false);
}
}
答案 0 :(得分:4)
您需要等待地图加载。使用此
((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.restaurant_map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(gps.getLatitude(), gps.getLongitude()), 15));
mGoogleMap = googleMap;
// Rest of the stuff you need to do with the map
}
});
}
答案 1 :(得分:0)
mMap = ((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.restaurant_map)).getMap();
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(gps.getLatitude(), gps.getLongitude()), 15));
他们所说的是用以下代码替换此代码,您可以尝试一下,看看它是否可行。祝你好运
((SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.restaurant_map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(gps.getLatitude(), gps.getLongitude()), 15));
mMap= googleMap;
// Rest of the stuff you need to do with the map
}
});
}
答案 2 :(得分:0)
您需要在onMapReady回调方法中等待GoogleMap实例。
@Override
public void onMapReady(GoogleMap googleMap) {
mGooglemap = googleMap;
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, 10));
}