0

制作我的第一个 android 应用程序,只是按照谷歌地图 api 版本 2 官方网站上的说明一步一步地进行。一切都按照它说的去做。包括获取 api 密钥、制作模拟器、安装所有 SDK、google 服务等。不知道为什么我在尝试启动应用程序时会收到错误消息:“不幸的是 googleMaps 已停止工作”。

不能说错误输出对我很有帮助,因为这是我的第一个 android 应用程序。我可以使用一些指导。

这是 LogCat 输出:

    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

除了更改清单文件之外,并没有真正为应用程序添加太多内容:

    <?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>
4

3 回答 3

0

使用此代码。你会得到输出。否则让我不要。使用 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();
    }
}
于 2014-09-26T09:55:02.003 回答
0

在为 11 以下的 API 创建应用程序时,您不能使用MapFragment,而是应该SupportMapFragment在 xml 和代码中使用。

切换到支持版本时,请记住扩展FragmentActivity而不是Activity使用getSupportFragmentManager()而不是getFragmentManager()

于 2013-09-15T10:42:57.297 回答
0

引起:android.app.Fragment$InstantiationException:无法实例化片段com.google.android.gms.maps.MapFragment:确保类名存在,是公共的,并且有一个公共的空构造函数引起:java.lang .ClassNotFoundException:com.google.android.gms.maps.MapFragment

问题似乎就在那里。那个类存在吗?是公开的吗?

于 2013-09-15T09:57:02.490 回答