我在我的应用程序中使用片段滑动菜单。我想为片段实现mapview。 但我得到了膨胀异常。如何解决它。 注意:在清单文件中,我也提供了所有权限。我也为此生成了API密钥。我尝试应用Supportmapfragment和sherlockFragment,但它需要LocationFragment而不是片段。但我只想在map片段中使用mapview。
我的mapview代码是..
public class LocationFragment extends Fragment {
private static View view;
MapFragment mapFragment;
GoogleMap map;
ImageView iv;
public LocationFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map_info, container,
false);
mapFragment = ((MapFragment) this.getActivity()
.getFragmentManager().findFragmentById(R.id.map));
iv = (ImageView) view.findViewById(R.id.iv);
map = mapFragment.getMap();
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(false);
MapsInitializer.initialize(this.getActivity());
} catch (InflateException e) {
Toast.makeText(getActivity(), "Problems inflating the view !",
Toast.LENGTH_LONG).show();
} catch (NullPointerException e) {
Toast.makeText(getActivity(), "Google Play Services missing !",
Toast.LENGTH_LONG).show();
}
return view;
}}
XML布局是..
<?xml version="1.0" encoding="utf-8"?>
<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" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
</RelativeLayout>
输出是.. 让视图膨胀的问题!
请帮忙。提前谢谢。
错误日志是:
08-25 18:04:10.420: W/dalvikvm(25577): threadid=1: thread exiting with uncaught exception (group=0x413ef9a8)
08-25 18:04:10.432: E/AndroidRuntime(25577): FATAL EXCEPTION: main
08-25 18:04:10.432: E/AndroidRuntime(25577): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.example.bulidersapp.LocationFragment.onCreateView(LocationFragment.java:24)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.Fragment.performCreateView(Fragment.java:1695)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.BackStackRecord.run(BackStackRecord.java:694)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.os.Handler.handleCallback(Handler.java:725)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.os.Looper.loop(Looper.java:153)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.ActivityThread.main(ActivityThread.java:5297)
08-25 18:04:10.432: E/AndroidRuntime(25577): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 18:04:10.432: E/AndroidRuntime(25577): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-25 18:04:10.432: E/AndroidRuntime(25577): at dalvik.system.NativeStart.main(Native Method)
08-25 18:04:10.432: E/AndroidRuntime(25577): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4323000 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" />
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.common.GooglePlayServicesUtil.t(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.maps.internal.q.B(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.maps.internal.q.A(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.maps.MapFragment$b.gW(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.dynamic.a.a(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.app.Activity.onCreateView(Activity.java:4734)
08-25 18:04:10.432: E/AndroidRuntime(25577): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
08-25 18:04:10.432: E/AndroidRuntime(25577): ... 19 more
清单文件添加项目......
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<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" />
<permission
android:name="com.example.bulidersapp.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.myapp.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
和内部申请
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="--my application app key---" />
答案 0 :(得分:0)
在您的AndroidManifest.xml
文件应用标签中添加google地图元标记,如下所示:
<application>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
</application>