android 代码中的空指针异常?

null pointer exceptions in android code?

2:10:25.524: E/AndroidRuntime(2019): FATAL EXCEPTION: main 01-06 12:10:25.524: E/AndroidRuntime(2019): Process: com.kandy, PID: 2019 01-06 12:10:25.524: E/AndroidRuntime(2019): java.lang.RuntimeException: Unable to start activity 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.access0(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): Caused by: 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)

我正在为此使用 google 个地图

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

好的,当您尝试查找 gmap 的视图时(您做对了),然后您立即尝试获取具有函数 get map() 的地图(这里您做错了),然后执行下一个获取空的 onCreate 方法) 就做下一个

gmap=((MapFragment)getFragmentManager().findFragmentById(R.id.map));
 gmap.getMap();

在查找事物的视图时,切勿使用方法(getMap() 或熟悉的方法)!

使用 SupportMapFragment 而不是 MapFragment。试试下面的代码片段

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

gmap = fm.getMap();