我正在尝试创建一个带有MapView
的布局以及其他项目当我尝试创建一个片段来保存视图时崩溃说它无法找到该类(是的我确实导入了google play服务图书馆)
错误:
03-08 13:48:01.593: E/AndroidRuntime(30614): android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.gms.maps.MapView
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-08 13:48:01.593: E/AndroidRuntime(30614): at com.ecm2.mobilemap.fragments.CurrentIncidentMapFrag.onCreateView(CurrentIncidentMapFrag.java:16)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.Fragment.performCreateView(Fragment.java:1695)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.BackStackRecord.run(BackStackRecord.java:682)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.os.Handler.handleCallback(Handler.java:725)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.os.Handler.dispatchMessage(Handler.java:92)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.os.Looper.loop(Looper.java:137)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-08 13:48:01.593: E/AndroidRuntime(30614): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 13:48:01.593: E/AndroidRuntime(30614): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 13:48:01.593: E/AndroidRuntime(30614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-08 13:48:01.593: E/AndroidRuntime(30614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-08 13:48:01.593: E/AndroidRuntime(30614): at dalvik.system.NativeStart.main(Native Method)
03-08 13:48:01.593: E/AndroidRuntime(30614): Caused by: java.lang.reflect.InvocationTargetException
03-08 13:48:01.593: E/AndroidRuntime(30614): at java.lang.reflect.Constructor.constructNative(Native Method)
03-08 13:48:01.593: E/AndroidRuntime(30614): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
03-08 13:48:01.593: E/AndroidRuntime(30614): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
03-08 13:48:01.593: E/AndroidRuntime(30614): ... 20 more
03-08 13:48:01.593: E/AndroidRuntime(30614): Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
03-08 13:48:01.593: E/AndroidRuntime(30614): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
03-08 13:48:01.593: E/AndroidRuntime(30614): at com.google.android.gms.maps.MapView.<init>(Unknown Source)
layout 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" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
和我的片段
public class CurrentIncidentMapFrag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saved){
View v = inflater.inflate(R.layout.current_incident_mapping_layout,container,false);
return v;
}
}
我知道有一个地图片段,但我想拥有的不仅仅是一张地图,我不希望它占据片段中的整个视图,所以带有包含所有内容的视图的片段是我唯一的选择。
如何让地图显示?
答案 0 :(得分:1)
你忘了把你的api密钥
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="***"
/>
并确保您在清单
中有这一行<uses-library android:name="com.google.android.maps" />
修改
如果删除onDestroyView()中的所有嵌套片段,则可以解决此问题。不知道这是否是一个合适的解决方案。
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
并在onCreateView()
中像往常一样对它们进行充气public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.map, container, false);
}
答案 1 :(得分:0)
我真的不明白,你说你想使用Google Map API V2。 但您正在尝试使用MapView对象,该对象是Google Map API V1对象。 如果你想在这里创建一个带有版本的地图:
这是我写的关于创建Google Map API V2的帖子。