gpt4 book ai didi

Android 谷歌地图 CameraUpdateFactory 未初始化

转载 作者:太空狗 更新时间:2023-10-29 16:13:19 25 4
gpt4 key购买 nike

我想在 map 上显示一个点作为我的位置,但是当我在 map 上设置相机时为什么会出现这个错误?

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();

我多次找到这个答案,但如果我的代码有问题,总是失败?请帮助谢谢。

map 的更多细节

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);
}
}

最佳答案

您需要等待 map 加载。用这个

((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
}
});
}

关于Android 谷歌地图 CameraUpdateFactory 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37574153/

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