选项卡上的 SupportMapFragment。我只能看到一次
Posted
技术标签:
【中文标题】选项卡上的 SupportMapFragment。我只能看到一次【英文标题】:SupportMapFragment on a tab. I can only see once 【发布时间】:2013-03-21 11:21:11 【问题描述】:我正在使用 FragmentTabHost 开发一个选项卡式应用程序。我的一个标签(片段)是一个地图(扩展片段),我只能在第一次选择它时看到,如果我选择其他标签并返回地图,应用程序崩溃......
应用必须兼容 API8
主要活动
package pack.unimaps2;
import com.google.android.gms.common.GooglePlayServicesUtil;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
public class MainActivity extends FragmentActivity
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState)
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Ir a..."),
Fragments.RutaHastaTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mapa").setIndicator("Mapa"),
Fragments.map.class, null);
mTabHost.addTab(mTabHost.newTabSpec("acercade").setIndicator("Acerca de..."),
Fragments.AcercaDeTab.class, null);
FragmentsActivity(省略第一个片段)
package pack.unimaps2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.SupportMapFragment;
public class Fragments extends FragmentActivity
public static class map extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View v = inflater.inflate(R.layout.map, container, false);
return v;
public static class AcercaDeTab extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View v = inflater.inflate(R.layout.tab_acercade, container, false);
return v;
以及创建地图片段视图的xml(map.xml):
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_
android:layout_
class="com.google.android.gms.maps.SupportMapFragment"/>
日志
03-30 21:07:49.180: E/AndroidRuntime(16464): FATAL EXCEPTION: main
03-30 21:07:49.180: E/AndroidRuntime(16464): android.view.InflateException: Binary XML file line #1: Error inflating class fragment
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-30 21:07:49.180: E/AndroidRuntime(16464): at pack.unimaps2.Fragments$map.onCreateView(Fragments.java:73)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.os.Handler.handleCallback(Handler.java:615)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.os.Looper.loop(Looper.java:137)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-30 21:07:49.180: E/AndroidRuntime(16464): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 21:07:49.180: E/AndroidRuntime(16464): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 21:07:49.180: E/AndroidRuntime(16464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-30 21:07:49.180: E/AndroidRuntime(16464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-30 21:07:49.180: E/AndroidRuntime(16464): at dalvik.system.NativeStart.main(Native Method)
03-30 21:07:49.180: E/AndroidRuntime(16464): Caused by: java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f040007, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
03-30 21:07:49.180: E/AndroidRuntime(16464): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-30 21:07:49.180: E/AndroidRuntime(16464): ... 18 more
不知道是什么问题,谢谢你的帮助...
【问题讨论】:
崩溃的Logcat好吗? 请在此处添加错误.. 请看下面的更新,请注意inflater的变化 请检查下面我的解决方案并注意代码中的注释,对 AcercaDeTab 类执行相同操作 我也有同样的问题。有人知道这个问题的答案吗? 【参考方案1】:还在寻找答案?
您不能一直在每次 onCreateView 时都膨胀布局。使用类似的东西:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
if (view != null && this.previousContainer == container)
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
else
try
view = inflater.inflate(R.layout.map, container, false);
this.previousContainer = container;
catch (InflateException e)
Log.w("InflateException happened ou nous", e.getMessage());
return view;
我设法防止了这种情况,并且在重新创建片段时也丢失了地图(例如从垂直变为横向时)
【讨论】:
【参考方案2】:片段应该有一个空的构造函数
如下:
public static class map extends Fragment
public map()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View v = inflater.inflate(R.layout.map, container, false);
// Change the previous line to this:
View v = inflater.inflate(R.layout.map, null);
return v;
【讨论】:
【参考方案3】:问题是您正试图在存在旧视图的情况下创建新视图;只需简单地实现此代码即可。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
if (v != null)
ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null)
parent.removeView(v);
try
v = inflater.inflate(R.layout.map_fragment, container, false);
catch (InflateException e)
// map is already there, just return view as it is
return v;
【讨论】:
【参考方案4】:此解决方案有效,但可能不是您的情况。我遇到了同样的问题,但在ViewPager
中使用了FragmentPagerAdapter
,而不是TabHost
。
我所做的是让 ViewPager 始终存储所有页面:
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(adapter);
所以我的带有地图片段的标签(在我的例子中是第 0 页)将永远不会被删除和重新创建,从而摆脱了这个问题。
value 2
表示它将在内存中保留当前页的前两页和后两页。当您有 3 个选项卡时,这就足够了。如果您有更多选项卡,请随意增加它,但您的应用程序的内存消耗也会增加
【讨论】:
以上是关于选项卡上的 SupportMapFragment。我只能看到一次的主要内容,如果未能解决你的问题,请参考以下文章