2:10:25.524:E / AndroidRuntime(2019):致命异常:主01-06 12:10:25.524:E / AndroidRuntime(2019):处理:com.kandy,PID:2019 01-06 12:10:25.524:E / AndroidRuntime(2019): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.kandy / com.kandy.citymap}: java.lang.NullPointerException 01-06 12:10:25.524: E / AndroidRuntime(2019):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 01-06 12:10:25.524:E / AndroidRuntime(2019):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 01-06 12:10:25.524:E / AndroidRuntime(2019):at android.app.ActivityThread.access $ 800(ActivityThread.java:135)01-06 12:10:25.524:E / AndroidRuntime(2019):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 01-06 12:10:25.524:E / AndroidRuntime(2019):at android.os.Handler.dispatchMessage(Handler.java:102)01-06 12:10:25.524:E / AndroidRuntime(2019):at android.os.Looper.loop(Looper.java:136)01-06 12:10:25.524: E / AndroidRuntime(2019):at android.app.ActivityThread.main(ActivityThread.java:5017)01-06 12:10:25.524:E / AndroidRuntime(2019):at java.lang.reflect.Method.invokeNative(Native Method)01-06 12:10:25.524:E / AndroidRuntime(2019):at java.lang.reflect.Method.invoke(Method.java:515)01-06 12:10:25.524: E / AndroidRuntime(2019):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 01-06 12:10:25.524:E / AndroidRuntime(2019):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)01-06 12:10:25.524:E / AndroidRuntime(2019):at dalvik.system.NativeStart.main(Native Method)01-06 12:10:25.524: E / AndroidRuntime(2019):引起:java.lang.NullPointerException 01-06 12:10:25.524:E / AndroidRuntime(2019):at com.kandy.citymap.onCreate(citymap.java:25)01-06 12:10:25.524: E / AndroidRuntime(2019):at android.app.Activity.performCreate(Activity.java:5231)01-06 12:10:25.524:E / AndroidRuntime(2019):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 01-06 12:10:25.524:E / AndroidRuntime(2019):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
我正在使用谷歌地图这个
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.os.Bundle;
public class citymap extends Activity {
GoogleMap gmap;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.citymap);
gmap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);//line 25
gmap.setMyLocationEnabled(true);
gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(7.2964, 80.6350),12));
gmap.addMarker(new MarkerOptions().position(new LatLng(7.2964, 80.6350)).title("Kandy"));
}}
答案 0 :(得分:0)
当你试图找到gmap的视图(你的做法正确)时,你可以直接尝试获取一个带有函数get map()
的地图(这里你做错了)做下一个
gmap=((MapFragment)getFragmentManager().findFragmentById(R.id.map));
gmap.getMap();
当你找到你的东西的视图时,永远不要使用方法(getMap()或熟悉的东西!)
答案 1 :(得分:0)
使用SupportMapFragment代替MapFragment。请尝试以下代码段
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
gmap = fm.getMap();