gpt4 book ai didi

android - 将Google map 插入我的Android应用中

转载 作者:行者123 更新时间:2023-11-29 16:08:40 25 4
gpt4 key购买 nike

您好,我将需要我的Android应用程序帮助以将google maps插入该应用程序,但是每次我执行该应用程序都会崩溃,并且如果有人可以帮助我找到问题,我将在下面插入我的代码与代码。

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 **

03-21 12:23:03.633:E / Trace(958):打开跟踪文件时出错:没有这样的文件或目录(2)
03-21 21:23:04.422:D / AndroidRuntime(958):关闭VM
03-21 12:23:04.432:W / dalvikvm(958):threadid = 1:线程以未捕获的异常退出(group = 0x40a13300)
03月21日12:23:04.452:E / AndroidRuntime(958):致命异常:主要
03月21日12:23:04.452:E / AndroidRuntime(958):java.lang.RuntimeException:无法启动活动ComponentInfo {com.maret.maps / com.maret.maps.Main}:android.view.InflateException:二进制XML文件第7行:错误夸大类片段
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.ActivityThread.access $ 600(ActivityThread.java:130)
03-21 12:23:04.452:E / AndroidRuntime(958):位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195)
03-21 12:23:04.452:E / AndroidRuntime(958):位于android.os.Handler.dispatchMessage(Handler.java:99)
03-21 12:23:04.452:E / AndroidRuntime(958):位于android.os.Looper.loop(Looper.java:137)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.ActivityThread.main(ActivityThread.java:4745)
03-21 12:23:04.452:E / AndroidRuntime(958):at java.lang.reflect.Method.invokeNative(本机方法)
03-21 21:23:04.452:E / AndroidRuntime(958):at java.lang.reflect.Method.invoke(Method.java:511)
03月21日12:23:04.452:E / AndroidRuntime(958):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
03-21 21:23:04.452:E / AndroidRuntime(958):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03月21日12:23:04.452:E / AndroidRuntime(958):在dalvik.system.NativeStart.main(本机方法)
03-21 12:23:04.452:E / AndroidRuntime(958):原因:android.view.InflateException:二进制XML文件第7行:夸大类片段的错误
03-21 12:23:04.452:E / AndroidRuntime(958):位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-21 21:23:04.452:E / AndroidRuntime(958):位于android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-21 12:23:04.452:E / AndroidRuntime(958):位于com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
03-21 12:23:04.452:E / AndroidRuntime(958):位于android.app.Activity.setContentView(Activity.java:1867)
03月21日12:23:04.452:E / AndroidRuntime(958):位于com.maret.maps.Main.onCreate(Main.java:12)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.Activity.performCreate(Activity.java:5008)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03月21日12:23:04.452:E / AndroidRuntime(958):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-21 12:23:04.452:E / AndroidRuntime(958):...还有11个
03-21 12:23:04.452:E / AndroidRuntime(958):原因:java.lang.ClassCastException:com.google.android.gms.maps.SupportMapFragment无法转换为android.app.Fragment
03-21:12:23:04.452:E / AndroidRuntime(958):位于android.app.Fragment.instantiate(Fragment.java:577)
03-21 21: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

最佳答案

这是我写的有关如何在应用程序中创建Map片段的博客文章:

Google Maps 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类找不到。

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

关于android - 将Google map 插入我的Android应用中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com