1

您好,我需要我的 Android 应用程序的帮助才能将谷歌地图插入应用程序,但每次我这样做时,应用程序都会崩溃,如果有人可以帮助我找到问题,我将在下面插入我的代码与代码。

Androidmanifest.xml

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

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

    <permission
    android:name="com.maret.toilet.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />


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


    <uses-permission android:name="com.maret.toilet.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/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.maret.toilet.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>
    <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="***MY_API_KEY****"/>
    </application>

    </manifest>

这是我的 activity_main.xml

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

    <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="My Location"
    android:onClick="getLocation"
    android:layout_weight="0"
    />

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

这是我的 MainActivity.java

    package com.maret.maps;

    import android.app.Activity;
    import android.os.Bundle;


     public class MainActivity extends Activity {

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

    }

我的错误日志

     03-19 08:41:45.552: E/Trace(641): error opening trace file: 
      No such file or directory    (2)
      03-19 08:41:45.861: D/AndroidRuntime(641): 
      Shutting down VM
      03-19 08:41:45.872: W/dalvikvm(641): 
      threadid=1: thread exiting with uncaught   exception (group=0x40a13300)
      03-19 08:41:45.891: E/AndroidRuntime(641): FATAL EXCEPTION: main
      03-19 08:41:45.891: E/AndroidRuntime(641): 
     java.lang.RuntimeException: Unable to   start activity ComponentInfo{com.maret.toilet/com.maret.toilet.MainActivity}: android.view.InflateException: 
      Binary XML file line #15: Error inflating class fragment
      03-19 08:41:45.891: E/AndroidRuntime(641):    
      at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
      03-19 08:41:45.891: E/AndroidRuntime(641):    
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
      03-19 08:41:45.891: E/AndroidRuntime(641):    
      at android.app.ActivityThread.access$600(ActivityThread.java:130)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at android.os.Handler.dispatchMessage(Handler.java:99)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at android.os.Looper.loop(Looper.java:137)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at android.app.ActivityThread.main(ActivityThread.java:4745) 
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at java.lang.reflect.Method.invokeNative(Native Method)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at java.lang.reflect.Method.invoke(Method.java:511)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
     03-19 08:41:45.891: E/AndroidRuntime(641):     
     at   dalvik.system.NativeStart.main(Native Method)
    03-19 08:41:45.891: E/AndroidRuntime(641): 
    Caused by: android.view.InflateException:    
   Binary XML file line #15: Error inflating   class fragment
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at    android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at  android.view.LayoutInflater.inflate(LayoutInflater.java:489)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.app.Activity.setContentView(Activity.java:1867)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at com.maret.toilet.MainActivity.onCreate(MainActivity.java:12)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at   android.app.Activity.performCreate(Activity.java:5008)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
   03-19 08:41:45.891: E/AndroidRuntime(641):   ... 11 more
   03-19 08:41:45.891: E/AndroidRuntime(641): 
   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
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at   android.app.Fragment.instantiate(Fragment.java:584)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.app.Fragment.instantiate(Fragment.java:552)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.app.Activity.onCreateView(Activity.java:4656)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
   03-19 08:41:45.891: E/AndroidRuntime(641):   ... 21 more
   03-19 08:41:45.891: E/AndroidRuntime(641): 
   Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
   03-19 08:41:45.891: E/AndroidRuntime(641):   
   at android.app.Fragment.instantiate(Fragment.java:574)
   03-19 08:41:45.891: E/AndroidRuntime(641):   ... 24 more

这是新的布局和活动

这是 main.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" >

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

* 这是我的 main.java * *

          package com.maret.maps;

          import android.os.Bundle;
          import android.app.Activity;
          import android.view.Menu;

         public class Main extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

  }

这是应用程序崩溃时的新 LOGCAT **

SupportMapFragment 无法转换为 android.app.Fragment 03-21 12:23:04.452: E/AndroidRuntime(958): at android.app.Fragment.instantiate(Fragment.java:577) 03-21 12:23:04.452: E/AndroidRuntime(958): 在 android.app.Fragment.instantiate(Fragment.java:552) 03-21 12:23:04.452: E/AndroidRuntime(958): 在 android.app.Activity.onCreateView(Activity.java :4656) 03-21 12:23:04.452: E/AndroidRuntime(958): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 03-21 12:23:04.452: E/AndroidRuntime(958): ... 21 更多 03-21 12:23:07.973: I/Process(958): 发送信号。PID:958 SIG:9 在 android.app.Activity.onCreateView(Activity.java:4656) 03-21 12:23:04.452: E/AndroidRuntime(958): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 03-21 12 :23:04.452: E/AndroidRuntime(958): ... 21 更多 03-21 12:23:07.973: I/Process(958): 发送信号。PID:958 SIG:9 在 android.app.Activity.onCreateView(Activity.java:4656) 03-21 12:23:04.452: E/AndroidRuntime(958): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 03-21 12 :23:04.452: E/AndroidRuntime(958): ... 21 更多 03-21 12:23:07.973: I/Process(958): 发送信号。PID:958 SIG:9

4

2 回答 2

3

这是我写的关于如何在您的应用程序中创建地图片段的博客文章:

谷歌地图 API V2

从您的代码看来,您的权限未正确定义,您需要添加以下内容:

<permission android:name="com.maret.maps.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="com.maret.maps.permission.MAPS_RECEIVE"/>

请在崩溃时添加您的 logcat 堆栈跟踪,以便我们更清楚地了解问题所在。

更新:

从您收到的错误中:

 java.lang.RuntimeException: Unable to   start activity ComponentInfo{com.maret.toilet/com.maret.toilet.MainActivity}: android.view.InflateException: 
 Binary XML file line #15: Error inflating class fragment

似乎您没有google-play-services根据需要引用库,这就是片段无法膨胀的原因。MapFragment找不到课程。

请查看我在答案开头发布的指南,了解如何正确进行引用。

于 2013-03-19T07:46:46.303 回答
1

您的清单中的应用程序标记缺少此行

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

添加它,它应该修复崩溃

编辑:这仅适用于地图 api 版本 1

于 2013-03-19T07:41:55.340 回答