gpt4 book ai didi

Android fragment 谷歌地图 V2 : Error inflating class fragment

转载 作者:行者123 更新时间:2023-11-30 03:10:32 25 4
gpt4 key购买 nike

我正在开发一个使用 fragment 选项卡的应用程序,我的一个 fragment 使用 Google Maps V2

public class AppMapFragment extends Fragment { 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_map, container, false);

try {
// Loading map
initilizeMap();

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

addMarkers();
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
Location location = locationManager.getLastKnownLocation(provider);
if (location!=null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
myPos = new LatLng (latitude, longitude);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPos, 15));
} else {
// Move the camera instantly to London with a zoom of 15.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LONDON, 15));
}

// Zoom in, animating the camera.
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

return rootView;
}

我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#17df0d">

<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 version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.networkme"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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"
android:theme="@style/AppTheme" >
<activity
android:name="example.networkme.activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name="example.networkme.activities.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<action android:name="android.intent.action.MAIN" />

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

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name="example.networkme.activities.SettingsActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name="example.networkme.activities.InstagramLoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>

<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<activity
android:name="example.networkme.activities.AppSettingsActivity"
android:label="@string/settings" />

<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />

<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC0LIYgIxPdlXkb9o7H5ppICC-qTUAykJE" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

</application>

</manifest>

错误:

FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at example.networkme.fragments.AppMapFragment.onCreateView(AppMapFragment.java:41)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)

我只复制了部分错误,因为它相当长:(。每当我尝试运行应用程序时都会发生此错误。它引用的行是 View rootView = inflater.inflate(R.layout .fragment_map, container, false);。非常感谢任何帮助!提前致谢

最佳答案

看看这个实现,你应该像你已经做的那样,创建一个带有 map 的 fragment :

public class MapFragment extends Fragment

创建布局:ma​​pview.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapview"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraZoom="12"
map:mapType="normal"
map:uiZoomControls="false"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiZoomGestures="true"
map:uiTiltGestures="false" />

然后在 fragment 中这样做:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.mapview, container, false);
}

然后获取 map 实例:

private GoogleMap getGoogleMap() {
if (map == null && getActivity() != null && getActivity().getSupportFragmentManager()!= null) {
SupportMapFragment smf = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview);
if (smf != null) {
map = smf.getMap();
}
}
return map;
}

关于Android fragment 谷歌地图 V2 : Error inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21068588/

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