简体   繁体   中英

unfortunately app has stopped working, first android app, using google maps api

Making my first android app, just followed the instructions on the google maps api version 2 official site step by step. Did everything as it said. Including getting the api key, making emulators, installing all the SDK, google services etc.. Not sure why I get an error message as soon as I attempt to start the application: "Unfortunately googleMaps has stopped working".

Can't say the error output is very helpful to me as this is my first android app. I could use some guidance.

Here is LogCat output:

    09-15 11:26:39.755: I/Process(959): Sending signal. PID: 959 SIG: 9
09-15 11:26:42.685: D/AndroidRuntime(972): Shutting down VM
09-15 11:26:42.685: W/dalvikvm(972): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
09-15 11:26:42.715: E/AndroidRuntime(972): FATAL EXCEPTION: main
09-15 11:26:42.715: E/AndroidRuntime(972): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.MapActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.os.Looper.loop(Looper.java:137)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-15 11:26:42.715: E/AndroidRuntime(972):  at java.lang.reflect.Method.invokeNative(Native Method)
09-15 11:26:42.715: E/AndroidRuntime(972):  at java.lang.reflect.Method.invoke(Method.java:511)
09-15 11:26:42.715: E/AndroidRuntime(972):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-15 11:26:42.715: E/AndroidRuntime(972):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-15 11:26:42.715: E/AndroidRuntime(972):  at dalvik.system.NativeStart.main(Native Method)
09-15 11:26:42.715: E/AndroidRuntime(972): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-15 11:26:42.715: E/AndroidRuntime(972):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Activity.setContentView(Activity.java:1835)
09-15 11:26:42.715: E/AndroidRuntime(972):  at com.example.googlemaps.MapActivity.onCreate(MapActivity.java:12)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Activity.performCreate(Activity.java:4465)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
09-15 11:26:42.715: E/AndroidRuntime(972):  ... 11 more
09-15 11:26:42.715: E/AndroidRuntime(972): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Fragment.instantiate(Fragment.java:572)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Fragment.instantiate(Fragment.java:540)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Activity.onCreateView(Activity.java:4235)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
09-15 11:26:42.715: E/AndroidRuntime(972):  ... 20 more
09-15 11:26:42.715: E/AndroidRuntime(972): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
09-15 11:26:42.715: E/AndroidRuntime(972):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
09-15 11:26:42.715: E/AndroidRuntime(972):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-15 11:26:42.715: E/AndroidRuntime(972):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-15 11:26:42.715: E/AndroidRuntime(972):  at android.app.Fragment.instantiate(Fragment.java:562)
09-15 11:26:42.715: E/AndroidRuntime(972):  ... 23 more
09-15 11:31:42.784: I/Process(972): Sending signal. PID: 972 SIG: 9

Didn't really add much to the application except for changing the manifest file:

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

    <!-- google api so we can use google maps -->
    <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="my api key"/>

    <!-- Internet access to download maps -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- Network state access to check if we can access internet -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <!-- save catched maps to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- google services -->
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

    <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->

    <!-- location based on WIFI/GSM --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <!-- location based on GPS -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <!-- OpenGL ES version 2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>


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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.googlemaps.MapActivity"
            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>

Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment

The problem seems to be there. Does that class exist? Is it public?

When creating an application for API under 11, you cannot use MapFragment and should instead use SupportMapFragment in both xml and code.

When switching to support version, remember to extend FragmentActivity instead of Activity and use getSupportFragmentManager() instead of getFragmentManager() .

Use this code. you will get output. else let me no. use API 17

public class MainActivity extends Activity {

    private GoogleMap mymaps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        try {
            if (mymaps == null)
                mymaps = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();
            }

        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    protected void onResume() {
        super.onResume();
        init();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM