小编典典

Android(google api v2)上的简单地图应用。不起作用。错误

java

我尝试在Android上编写一个简单的地图应用程序,但唯一的结果就是错误。我尝试以应有的方式对其进行编程,但仍然无法正常工作。

主要活动

package com.example.guidebook;

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

public class GuidebookActivity extends Activity {

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

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

}

.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />
    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

表现

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
   <permission
       android:name="com.example.guidebook.permission.MAPS_RECEIVE" 
       android:protectionLevel="signature"/>

  <uses-permission android:name="com.example.guidebook.permission.MAPS_RECEIVE"/> 
  <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"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <!-- Maps API needs OpenGL ES 2.0. -->
  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.maps.v2.API_KEY"
    android:value="AI-------------------"/>
        <activity
            android:name="com.example.guidebook.GuidebookActivity"
            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: 12-09 10:35:44.200: D/AndroidRuntime(24663): Shutting down VM
12-09 10:35:44.200: W/dalvikvm(24663): threadid=1: thread exiting with uncaught exception (group=0x415d5ba8)
12-09 10:35:44.200: E/AndroidRuntime(24663): FATAL EXCEPTION: main
12-09 10:35:44.200: E/AndroidRuntime(24663): Process: com.example.guidebook, PID: 24663
12-09 10:35:44.200: E/AndroidRuntime(24663): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guidebook/com.example.guidebook.GuidebookActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.os.Looper.loop(Looper.java:136)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at java.lang.reflect.Method.invoke(Method.java:515)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at dalvik.system.NativeStart.main(Native Method)
12-09 10:35:44.200: E/AndroidRuntime(24663): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.Activity.setContentView(Activity.java:1929)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at com.example.guidebook.GuidebookActivity.onCreate(GuidebookActivity.java:12)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.Activity.performCreate(Activity.java:5231)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-09 10:35:44.200: E/AndroidRuntime(24663):    ... 11 more
12-09 10:35:44.200: E/AndroidRuntime(24663): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.google.android.gms.maps.SupportMapFragment that is not a Fragment
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.Fragment.instantiate(Fragment.java:585)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.Fragment.instantiate(Fragment.java:561)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.app.Activity.onCreateView(Activity.java:4778)
12-09 10:35:44.200: E/AndroidRuntime(24663):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-09 10:35:44.200: E/AndroidRuntime(24663):    ... 20 more
12-09 10:35:44.200: E/AndroidRuntime(24663): Caused by: java.lang.ClassCastException
12-09 10:35:44.200: E/AndroidRuntime(24663):    ... 24 more
12-09 10:36:10.520: D/AndroidRuntime(24870): Shutting down VM
12-09 10:36:10.520: W/dalvikvm(24870): threadid=1: thread exiting with uncaught exception (group=0x415d5ba8)
12-09 10:36:10.520: E/AndroidRuntime(24870): FATAL EXCEPTION: main
12-09 10:36:10.520: E/AndroidRuntime(24870): Process: com.example.guidebook, PID: 24870
12-09 10:36:10.520: E/AndroidRuntime(24870): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.guidebook/com.example.guidebook.GuidebookActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.os.Looper.loop(Looper.java:136)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at java.lang.reflect.Method.invoke(Method.java:515)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at dalvik.system.NativeStart.main(Native Method)
12-09 10:36:10.520: E/AndroidRuntime(24870): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.Activity.setContentView(Activity.java:1929)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at com.example.guidebook.GuidebookActivity.onCreate(GuidebookActivity.java:12)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.Activity.performCreate(Activity.java:5231)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
12-09 10:36:10.520: E/AndroidRuntime(24870):    ... 11 more
12-09 10:36:10.520: E/AndroidRuntime(24870): Caused by: java.lang.NullPointerException: name == null
12-09 10:36:10.520: E/AndroidRuntime(24870):    at java.lang.VMClassLoader.findLoadedClass(Native Method)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:350)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at java.lang.ClassLoader.loadClass(ClassLoader.java:487)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.Fragment.instantiate(Fragment.java:583)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.Fragment.instantiate(Fragment.java:561)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.app.Activity.onCreateView(Activity.java:4778)
12-09 10:36:10.520: E/AndroidRuntime(24870):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
12-09 10:36:10.520: E/AndroidRuntime(24870):    ... 20 more
12-09 10:36:12.420: I/Process(24870): Sending signal. PID: 24870 SIG: 9

阅读 215

收藏
2020-11-26

共1个答案

小编典典

在下面检查我的答案:

在您的xml文件中更改此 class="com.google.android.gms.maps.MapFragment" to android:name="com.google.android.gms.maps.SupportMapFragment"

将您的活动扩展到FragmentActivty并进行更改

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) .getMap(); to
map= ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap();

还要检查 if (map== null) { map= ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap();}

检查清单文件:

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

<permission
    android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" />

<uses-sdk
    android:minSdkVersion="12"
    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" />

<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name="com.example.googlemapsv2.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <!-- Google API Key -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your Google map API key" />
</application>

希望这可以帮助。

2020-11-26