gpt4 book ai didi

android - 在谷歌地图项目中膨胀类 fragment 时出错

转载 作者:行者123 更新时间:2023-11-29 17:52:07 25 4
gpt4 key购买 nike

我正在开发一个在屏幕上显示 Google map 的项目。我根据互联网帮助做了每一件事,我也按照教程进行操作,但我没有解决问题这是我的项目..

主要 Activity .java

package com.appscourt.jogging.track.map.health;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

// Google Map
private GoogleMap googleMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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();
}

}

主.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> \

list .xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appscourt.jogging.track.map.health"
android:versionCode="1"
android:versionName="1.0" >

<permission
android:name="com.appscourt.jogging.track.map.health.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />

<uses-permission android:name="com.appscourt.jogging.track.map.health.permission.MAPS_RECEIVE" />

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name="com.appscourt.jogging.track.map.health.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>

<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyD9tu2CyVniM72vTAQcocWNlJEaACQ-3rs" />
</application>

</manifest>

日志...

03-03 05:18:01.984: D/AndroidRuntime(1297): Shutting down VM
03-03 05:18:01.984: W/dalvikvm(1297): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-03 05:18:02.004: E/AndroidRuntime(1297): FATAL EXCEPTION: main
03-03 05:18:02.004: E/AndroidRuntime(1297): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appscourt.jogging.track.map.health/com.appscourt.jogging.track.map.health.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.os.Looper.loop(Looper.java:137)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-03 05:18:02.004: E/AndroidRuntime(1297): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 05:18:02.004: E/AndroidRuntime(1297): at java.lang.reflect.Method.invoke(Method.java:525)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 05:18:02.004: E/AndroidRuntime(1297): at dalvik.system.NativeStart.main(Native Method)
03-03 05:18:02.004: E/AndroidRuntime(1297): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.Activity.setContentView(Activity.java:1895)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.appscourt.jogging.track.map.health.MainActivity.onCreate(MainActivity.java:20)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.Activity.performCreate(Activity.java:5133)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-03 05:18:02.004: E/AndroidRuntime(1297): ... 11 more
03-03 05:18:02.004: E/AndroidRuntime(1297): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4242000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.maps.MapFragment$b.ex(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.dynamic.a.a(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.app.Activity.onCreateView(Activity.java:4745)
03-03 05:18:02.004: E/AndroidRuntime(1297): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
03-03 05:18:02.004: E/AndroidRuntime(1297): ... 21 more

谁能帮帮我...

提前致谢

最佳答案

我的 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=".CarLiveTrackingMapScreen" >

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

</RelativeLayout>

类文件你应该扩展FragmentActivity

public class LiveTrackingActivity extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.live_tracking_activity);
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager
.findFragmentById(R.id.tracking_screen_map);
myMap = mySupportMapFragment.getMap();
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setRotateGesturesEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
}
}

将此代码添加到您的 XML 文件中

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

<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="AIzaSyB6krIcZoYtN73-l8ijuwkbCw-yhLfp7DY" />

如有任何疑问,请随时询问。

关于android - 在谷歌地图项目中膨胀类 fragment 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22144190/

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