当前位置: 首页 > 面试题库 >

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

罗奇文
2023-03-14
问题内容

我尝试在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

问题答案:

在下面检查我的答案:

在您的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>

希望这可以帮助。



 类似资料:
  • 这是我的密码。这很简单,但当我点击按钮“button1”时,什么都没有发生。我做错了什么? 第二个-- 第三个-- 布局-- 新Logcat:11-29 12:15:39.552:D/gralloc_goldfish(613):检测到没有GPU仿真的模拟器。11-29 12:15:40.062:I/编舞(613):跳过51帧!应用程序可能在其主线程上做了太多工作。11-29 12:16:08.90

  • 我有一个“排行榜”/“记分板”,共有四张表格,我需要根据第一个总分(第2列)和第三个总得分(第3列)进行自动排序。这些列在所有四张图纸上都是相同的。 过去,当记分牌仅限于一张纸时,我使用了一个非常简单的脚本,但后来我将其扩展到同一个文档中的不同纸页上的前十名、前四名和前二名。 我遇到的问题是:当脚本更新一个工作表时,其他工作表似乎完全停止工作;换句话说,脚本中断了。 我能得到一些建议吗?我已经在这

  • 我有一个bean,它在XML中是一个类型化的-。但是当我尝试使用时,它并没有像预期的那样工作。我还尝试了,正如堆栈溢出中的一个相关问题所建议的那样,这也不起作用。 是否有一种方法可以查看xml中的bean是否被正确地实例化。我得到的属性服务值为。我已经为这个工作了一天了。仍然无法解开这个谜题。虽然我搜遍了spring reference和stackoverflow(奇怪的是,spring的地图Au

  • 我到处都搜索了,但找不到答案,我怎么能发出HTTP请求?我尝试了几种可能性,但都不起作用。我总是出错。 02-27 17:52:58.611 13571-13571/com.example.app E/AndroidRuntime﹕ 致命异常:主java.lang.IllegalStateException:无法在android.view.view$1.onClick(view.java:3838

  • 问题内容: 我一直在试图弄清楚为什么我按下按钮时布尔值没有改变,而当我手动改变它时却起作用了,但是它什么也没做。我试图按照教程学习这个词,但是它们不起作用。有人可以指出我要去哪里了吗? 感谢您的所有帮助,当我尝试建议的代码但没有成功时,我返回并更改了菜单。以前,我使用onClick制作了一个按钮来创建菜单,当使用代码创建图标时,我之前编写的代码可以正常工作。希望这对我以外的人有帮助,所以我不会觉得

  • 我是android开发的新手:) 我正在尝试创建一个简单的应用程序:一个带有地图的activity。我把我的HTC1X连接到电脑上,当我试图运行应用程序(通过intellij IDEA)时,它崩溃了。 我已经在网上(和stackoverflow)寻找线索,但没有找到任何线索。例如:  -Google Maps v2-error  -Google Map-RuntimeException-error