无法启动活动java.lang.NullPointerException

时间:2013-04-30 09:11:15

标签: java android

我正在尝试在我的Android应用中实施谷歌地图v2并且我不断收到以下错误:

04-30 04:54:24.839: E/AndroidRuntime(2006): FATAL EXCEPTION: main
04-30 04:54:24.839: E/AndroidRuntime(2006): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.northeastvision.summit/org.northeastvision.summit.OurLocation}: java.lang.NullPointerException
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.os.Looper.loop(Looper.java:137)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at android.app.ActivityThread.main(ActivityThread.java:4898)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at java.lang.reflect.Method.invoke(Method.java:511)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
04-30 04:54:24.839: E/AndroidRuntime(2006):     at dalvik.system.NativeStart.main(Native Method)
04-30 04:54:24.839: E/AndroidRuntime(2006): Caused by: java.lang.NullPointerException
04-30 04:54:24.839: E/AndroidRuntime(2006):     at org.northeastvision.summit.OurLocation.onCreate(OurLocation.java:28)

我的XML文件包含一个片段,我的sdk min是11

 <fragment 
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment" />

我的Java文件:

public class OurLocation extends FragmentActivity {
    private GoogleMap mMap;
    private final LatLng SOLIDROCK = new LatLng(39.781056, -74.914503);

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.our_location);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

Line 28     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        gotoview();

    }


    private void gotoview() {
        // TODO Auto-generated method stub
        //CameraUpdate update = CameraUpdateFactory.newLatLng(SOLIDROCK);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(SOLIDROCK, 16);
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mMap.animateCamera(update);
    }

错误从第28行生成

2 个答案:

答案 0 :(得分:0)

试试这个

public class OurLocation extends com.google.android.gms.maps.SupportMapFragment {
    private GoogleMap mMap;
    private final LatLng SOLIDROCK = new LatLng(39.781056, -74.914503);

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.our_location);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        gotoview();

    }


    private void gotoview() {
        // TODO Auto-generated method stub
        //CameraUpdate update = CameraUpdateFactory.newLatLng(SOLIDROCK);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(SOLIDROCK, 16);
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
        mMap.animateCamera(update);
    }

答案 1 :(得分:0)

使用这种方式:

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

map = mapFrag.getMap();

检查此class

希望您在项目中添加了此android-support-v4.jar ...