Android - 片段内的MapView

时间:2014-05-25 10:54:12

标签: android google-maps-android-api-2

我遵循了许多教程并在这里阅读了很多问题,也许我有些困惑。

我正在开发一个应用程序,它通过操作栏显示MainActivity,将活动分成3个选项卡(每个选项卡都是一个片段)。在第一个标签上,我想显示一个Google地图(使用MapView,我猜它比MapFragment更容易),但我在实例化地图时面临NullPointerException。这是logcat:

05-25 06:14:26.740: E/AndroidRuntime(918): FATAL EXCEPTION: main
05-25 06:14:26.740: E/AndroidRuntime(918): Process: com.example.mypackage, PID: 918
05-25 06:14:26.740: E/AndroidRuntime(918): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypackage/com.example.mypackage.activities.MainActivity}: java.lang.NullPointerException
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.os.Looper.loop(Looper.java:136)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.main(ActivityThread.java:5017)
05-25 06:14:26.740: E/AndroidRuntime(918):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 06:14:26.740: E/AndroidRuntime(918):  at java.lang.reflect.Method.invoke(Method.java:515)
05-25 06:14:26.740: E/AndroidRuntime(918):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-25 06:14:26.740: E/AndroidRuntime(918):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-25 06:14:26.740: E/AndroidRuntime(918):  at dalvik.system.NativeStart.main(Native Method)
05-25 06:14:26.740: E/AndroidRuntime(918): Caused by: java.lang.NullPointerException
05-25 06:14:26.740: E/AndroidRuntime(918):  at com.example.mypackage.fragments.MapFragment.onCreateView(MapFragment.java:30)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.Activity.performStart(Activity.java:5241)
05-25 06:14:26.740: E/AndroidRuntime(918):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-25 06:14:26.740: E/AndroidRuntime(918):  ... 11 more

这是MapFragment

public class MapFragment extends Fragment {

    MapView mapView;
    GoogleMap map;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_map, container, false);
        // Gets the MapView from the XML layout and creates it
        mapView = (MapView) v.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);

        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls

        MapsInitializer.initialize(this.getActivity());

        // Updates the location and zoom of the MapView
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(0, 0), 10);
        map.animateCamera(cameraUpdate);
        return v;
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
}

因此产生错误的调用(第30行)是

map.getUiSettings().setMyLocationButtonEnabled(false);

最后,这是fragment_map.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

如果有任何帮助,我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mypackage"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my_api_key" />

        <activity
            android:name="com.example.mypackage.activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

任何人都知道为什么?感谢。

1 个答案:

答案 0 :(得分:1)

在android文档中,它说的是MapView.getMap():

如果地图视图尚未就绪,则为空。当Google Play服务不可用时,就会发生这种情况。如果之后Google Play服务可用,则再次调用此方法将初始化并返回GoogleMap。

您是否在启动应用的设备上安装了Google Play服务应用程序?