简体   繁体   English

选项卡上的SupportMapFragment。 我只能看一次

[英]SupportMapFragment on a tab. I can only see once

I'm working on a tabbed application using FragmentTabHost. 我正在使用FragmentTabHost处理选项卡式应用程序。 One of my tabs(Fragments) is a map (extends Fragment), and I can only see the first time I select it, If I select other tab and back to map, app crashes... 我的一个标签(片段)是一个地图(扩展片段),我只能看到我第一次选择它,如果我选择其他标签并返回地图,应用程序崩溃...

The app must be compatible with API8 该应用必须与API8兼容

Main activity 主要活动

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 (Omitted first fragment) 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;
        }

    }
}

And the xml to create the view of map Fragment (map.xml): 并且xml创建了地图Fragment(map.xml)的视图:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/map"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 class="com.google.android.gms.maps.SupportMapFragment"/>

Log 日志

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

Don't know what is wrong, thank you for your help... 不知道有什么不对,谢谢你的帮助......

Still looking for an answer? 还在寻找答案吗?

You can't inflate the layout everytime onCreateView all the time. 每次onCreateView都不能给布局充气。 Use something along the lines of: 使用以下内容:

@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;
}

I managed to prevent that and also losing the map when recreating the Fragment (when changing from vertical to landscape for instance) 我设法防止这种情况,并在重新创建片段时丢失地图(例如,当从垂直变为景观时)

Fragments should have an empty constructor 片段应该有一个空构造函数

as follows: 如下:

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;
    }

}

the problem is that you are trying to make a new view where there's an old one exist; 问题是你正在尝试创建一个旧视图,其中存在旧视图; just simply implement this code. 只需简单地实现此代码。

    @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;
}

this solution works, but may not be your case. 这个解决方案有效,但可能不是你的情况。 I had the same problem but using a FragmentPagerAdapter inside a ViewPager , not a TabHost . 我有同样的问题,但使用FragmentPagerAdapter一个内部ViewPager ,不是TabHost

What I did is to make the ViewPager store all pages all the time: 我所做的是让ViewPager始终存储所有页面:

viewPager.setOffscreenPageLimit(2); 
viewPager.setAdapter(adapter);

so my tab with the map fragment (page 0 in my case) will be never deleted and re-created, getting rid of the problem. 所以我的选项卡与地图片段(在我的情况下为第0页)将永远不会被删除和重新创建,摆脱问题。

value 2 means that it will keep in memory two pages before and two pages after the current one. 2意味着它将在前两页中保留在内存中,并在当前一页之后保留两页。 That will be enough when you have 3 tabs. 当你有3个标签时就足够了。 Feel free to increment it if you have more tabs, but your app's memory consumption will increase too 如果您有更多标签,请随意增加它,但您的应用程序的内存消耗也会增加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我看不到带有SupportMapFragment的底部按钮/ android设计支持库 - I can't see the bottom button / android design support library with SupportMapFragment Android SupportMapFragment无法在片段中看到地图 - Android SupportMapFragment can't see map inside a fragment 为什么我看不到tabHost的选项卡? - why i can't see the Tab of the tabHost? Android Sherlock ActionBar选项卡无法正确分离SupportMapFragment - Android Sherlock ActionBar Tab can't detach SupportMapFragment properly 如何在 Android Studio 中扩展 SupportMapFragment? - How can I extend SupportMapFragment in Android Studio? Android:无法加载标签。 说孩子已经有了父母 - Android: Can't load tab. Says child already has a parent 我正在尝试使用Samsung标签中的Google Plus登录。 应用程序未在我的设备上响应,但在模拟器上运行正常 - I am trying to login with Google Plus in Samsung tab. App not responding in my device but on emulator working fine Android,TabLayout(tabhost),切换标签会禁用第二个标签上的edittext。 我该如何预防? - Android, TabLayout (tabhost), switching tabs disables edittext on second tab. How do I prevent this? FragmentTabHost防止加载选项卡。 - FragmentTabHost prevent load loaded tab. 我可以在Google Maps v2上将SupportMapFragment更改为Mapview吗? - Can I change a SupportMapFragment to a Mapview on Google Maps v2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM