The app keeps crashing when adding "mMap = ((MapFragment) getFragmentManager (). FindFragmentById (R.id.map)). GetMap ();"

I am trying to learn how to develop a google android google maps app using google API V2. It works when I have not added

private GoogleMap mMap = null;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

      

in MyGoogleMapDemo.java but when I added them the app keeps crashing. I use

  • Google API 4.2
  • Android SDK Tools 21.1
  • Android SDK Platform-Tools 16.0.1
  • Eclipse SDK 3.7.1

and I added the android-support-v4.jar project to the project. I am looking forward

MyGoogleMapDemo.java

package org.lxh.demo;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MyGoogleMapDemo extends FragmentActivity 
{

private GoogleMap mMap = null;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

}

      

main.xml

 <?xml version="1.0" encoding="utf-8"?>
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"

    map:cameraBearing="112.5"
    map:cameraTargetLat="-33.796923"
    map:cameraTargetLng="150.922433"
    map:cameraTilt="30"
    map:cameraZoom="13"
    map:mapType="normal"
    map:uiCompass="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="true"
    map:uiTiltGestures="true"
    map:uiZoomControls="false"
    map:uiZoomGestures="true" />

      

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    <permission
        android:name="org.lxh.demo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="org.lxh.demo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCHCxLLeMk3zRL4TYICdbYjB6nQk-oCvRs" />

        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".MyGoogleMapDemo"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

      

log.txt

02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapUtilization:0.25
    02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapIdealFree:8388608
    02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapConcurrentStart:2097152
    02-17 22:56:52.585: W/dalvikvm(11229): threadid=1: thread exiting with uncaught exception (group=0x4106d498)
    02-17 22:56:52.585: E/AndroidRuntime(11229): FATAL EXCEPTION: main
    02-17 22:56:52.585: E/AndroidRuntime(11229): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.lxh.demo/org.lxh.demo.MyGoogleMapDemo}: java.lang.NullPointerException
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.access$600(ActivityThread.java:136)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.os.Handler.dispatchMessage(Handler.java:99)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.os.Looper.loop(Looper.java:137)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.main(ActivityThread.java:4797)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at dalvik.system.NativeStart.main(Native Method)
    02-17 22:56:52.585: E/AndroidRuntime(11229): Caused by: java.lang.NullPointerException
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at org.lxh.demo.MyGoogleMapDemo.onCreate(MyGoogleMapDemo.java:24)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.Activity.performCreate(Activity.java:5024)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
    02-17 22:56:52.585: E/AndroidRuntime(11229):    ... 11 more

      

+3


source to share


1 answer


You may have other messages in your LogCat related to your problem. I guess your problem is that you have the wrong item <fragment>

. You have:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    ... />

      



You shouldn't have both android:name

and class

and, of course, they shouldn't point to different classes. android:name

is the preferred approach. Since you are inheriting from FragmentActivity

, I am assuming that you really want to android:name

point to SupportMapFragment

, so make that change and get rid of the attribute class

. You can also get rid of the attributes xmlns:map

and all map:

at the beginning until you get the rest of your code.

+5


source







All Articles