0

尝试在谷歌地图上工作

  • 这是我使用谷歌地图 API 的第一个程序
  • 由于日志错误,我无法执行
  • 我需要纠正自己的地方

MainActivity.java

public class MainActivity extends Activity {

    // Google Map
    private GoogleMap googleMap;

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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="android.app.DialogFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

显现

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

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

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

    <!-- Required to show current location -->
    <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.example.googlemaps.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>

        <!-- Goolge Maps API Key -->
        <meta-data
            android:name="com.example.googlemaps.API_KEY"
            android:value="------------My-Key----------------" />
    </application>

</manifest>

在此处输入图像描述

UPDATED_XML

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

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

</RelativeLayout>

更新日志

10-17 10:57:50.547: I/dalvikvm(458): Could not find method com.example.googlemaps.MainActivity.getFragmentManager, referenced from method com.example.googlemaps.MainActivity.initilizeMap
10-17 10:57:50.547: W/dalvikvm(458): VFY: unable to resolve virtual method 4829: Lcom/example/googlemaps/MainActivity;.getFragmentManager ()Landroid/app/FragmentManager;
10-17 10:57:50.547: D/dalvikvm(458): VFY: replacing opcode 0x6e at 0x0004
10-17 10:57:50.547: D/dalvikvm(458): VFY: dead code 0x0007-0028 in Lcom/example/googlemaps/MainActivity;.initilizeMap ()V
10-17 10:57:50.627: D/AndroidRuntime(458): Shutting down VM
10-17 10:57:50.627: W/dalvikvm(458): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-17 10:57:50.657: E/AndroidRuntime(458): FATAL EXCEPTION: main
10-17 10:57:50.657: E/AndroidRuntime(458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemaps/com.example.googlemaps.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.os.Looper.loop(Looper.java:123)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-17 10:57:50.657: E/AndroidRuntime(458):  at java.lang.reflect.Method.invokeNative(Native Method)
10-17 10:57:50.657: E/AndroidRuntime(458):  at java.lang.reflect.Method.invoke(Method.java:507)
10-17 10:57:50.657: E/AndroidRuntime(458):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-17 10:57:50.657: E/AndroidRuntime(458):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-17 10:57:50.657: E/AndroidRuntime(458):  at dalvik.system.NativeStart.main(Native Method)
10-17 10:57:50.657: E/AndroidRuntime(458): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-17 10:57:50.657: E/AndroidRuntime(458):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.Activity.setContentView(Activity.java:1657)
10-17 10:57:50.657: E/AndroidRuntime(458):  at com.example.googlemaps.MainActivity.onCreate(MainActivity.java:19)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-17 10:57:50.657: E/AndroidRuntime(458):  ... 11 more
10-17 10:57:50.657: E/AndroidRuntime(458): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.googlemaps-2.apk]
10-17 10:57:50.657: E/AndroidRuntime(458):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-17 10:57:50.657: E/AndroidRuntime(458):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-17 10:57:50.657: E/AndroidRuntime(458):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
10-17 10:57:50.657: E/AndroidRuntime(458):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-17 10:57:50.657: E/AndroidRuntime(458):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
10-17 10:57:50.657: E/AndroidRuntime(458):  ... 20 more

有任何想法吗

谢谢

4

2 回答 2

0

您必须在您的 xml 布局中声明 map 的片段类,如下所示: 用于GoogleMap在您的片段中加载。

您是否在您的应用程序中提到了添加地图

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

编辑:

该错误是因为您的应用程序包含低于 12 的 API 级别。请注意,下面的代码仅用于在面向 Android API 12 或更高版本的应用程序中测试您的设置,下面的代码不应在生产应用程序中使用。

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

所以改为尝试如下:

   mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
于 2013-10-17T05:20:26.707 回答
0

只需按照 The Links 作为您的第一个地图应用程序。

http://mobiforge.com/developing/story/developing-with-google-maps-v2-android 点击链接并检查 1)确保你有谷歌地图 v2 支持库(添加 android-support-v4)和

public class MainActivity extends FragmentActivity {

2)检查和修改Manifest 3)在布局中

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

4) 在活动中

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            googleMap = fm.getMap();

它将解决您的问题。

于 2013-10-17T05:42:52.053 回答