"重复ID,标签空,或父ID为0x0与另一个片段QUOT;添加第二个片段ActionBarActivity时 [英] "Duplicate id, tag null, or parent id 0x0 with another fragment" when adding second Fragment to ActionBarActivity

查看:110
本文介绍了"重复ID,标签空,或父ID为0x0与另一个片段QUOT;添加第二个片段ActionBarActivity时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个java.lang.IllegalArgumentException:如果二进制XML文件中的行#3:重复ID 0x7f05003f,标记为空,或父ID为0x0与另一片段errror当我尝试添加一个新的片段,我的主要ActionBarActivity。

I am getting a "java.lang.IllegalArgumentException: Binary XML file line #3: Duplicate id 0x7f05003f, tag null, or parent id 0x0 with another fragment" errror when I try to add a new Fragment in my main ActionBarActivity.

主要错误是android.view.InflateException:二进制XML文件中的行#3:错误充气类片段的错误,在我的PlaceDetailsFragment类返回行

The main error is a android.view.InflateException: Binary XML file line #3: Error inflating class fragment error, on the return line in my PlaceDetailsFragment class.

目标:要顶得上第一SupportMapFragment第二个片段,它覆盖约30%

Goal: To get a second Fragment on top the first SupportMapFragment, covering it for about 30%.

main.java

main.java

public class Main extends ActionBarActivity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        if (findViewById(R.id.fragment_container) != null) {

            if (savedInstanceState != null) {
                return;
            }

            FragmentTransaction mTransaction = getSupportFragmentManager()
                    .beginTransaction();
            SupportMapFragment mapFragmentSupportClass = new MapFragmentClass();
            mTransaction.add(R.id.fragment_container, mapFragmentSupportClass);
            mTransaction.commit();

            FragmentTransaction xTransaction = getSupportFragmentManager().beginTransaction();
            PlaceDetailsFragment placeDetailsFragment = new PlaceDetailsFragment();
            xTransaction.add(R.id.fragment_container, placeDetailsFragment);
            xTransaction.commit();
        }
    }

PlaceDetailsFragment.java这是在应用程序崩溃,在返回行

PlaceDetailsFragment.java This is where the app crashes, on the return line.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PlaceDetailsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.details, container, false);

    }
}

main.xml中

main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fragment_container"
             android:layout_width="match_parent"
             android:layout_height="match_parent" >

 </FrameLayout>

mapfragment.xml

mapfragment.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"
          android:name="com.google.android.gms.maps.MapFragment"
/>

details.xml

details.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="match_parent"
          android:layout_height="200dp"
          android:id="@+id/details"
          android:name="com.qstudios.whatsopenlate.PlaceDetailsFragment"
          android:tag="detailsTag"
/>

要尽我所知,我没有使用重复的ID,也没有标签,使叶片父ID片段与mapFragment,主要的fragment_container分享。为什么这是一个问题。是不是合乎逻辑的父母具有相同的ID?此外,这是不是一个NestedFragment,是否正确?我使用的是ActionBarActivity和的FrameLayout把片段

To the best of my knowledge, I'm not using duplicate id's nor tags, so that leaves the parent id the fragment is sharing with mapFragment, main's fragment_container. Why is this a problem. Isn't it logical a parent has the same ID? Also, this is not a NestedFragment, correct? I'm using an ActionBarActivity and a FrameLayout to put the fragments in.

此错误已经停止我的工作效率有2个小时了,而我发现谷歌是问题的答案要么约ActionBarSherlock,嵌套的片段,等没有人能提供一个简单的解释或例子就如何恰当地把2片在的FrameLayout。即使 https://developer.android.com/training/basics/fragments/片段ui.html 没有解释如何做到这一点。

This error has halted my productivity with 2 hours already, and all of the answers I found on google were either about ActionBarSherlock, Nested Fragments, etc. None could provide a simple explanation or example on how to appropriately put 2 fragments in a FrameLayout. Even https://developer.android.com/training/basics/fragments/fragment-ui.html doesn't explain how to do this.

任何人任何这方面的经验,知道我在做什么错了,我怎么能解决这个问题?

Anybody any experience with this, and knows what I'm doing wrong and how I could fix it?

在此先感谢!

推荐答案

这个问题,因为你已经或多或少地发现了自己,在这样的&LT;片断&gt; 标记用于details.xml。每Android的文档

The problem, as you've more or less discovered yourself, is in the way the <fragment> tag is used in details.xml. Per the Android documentation:

这是活动的布局XML可以包括&LT;片断&gt; 标签嵌入布局内片段实例

An activity's layout XML can include <fragment> tags to embed fragment instances inside of the layout.

基本上,你试图使用标签作为你的片段一个实际的布局,而所有它的意思做的是嵌入现有片段(已经有自己的布局)在其他一些观点。因此,正确的解决方案确实是搭建片段布局(如中提到的注释的RelativeLayout的),使得没有使用&LT;片断&gt;

Basically, you were trying to use the tag as an actual layout for your fragment, whereas all it's meant to do is embed an existing fragment (which already has its own layout) in some other view. Therefore, the proper solution is indeed to build a layout for the fragment (e.g. a RelativeLayout as mentioned in the comments), making no use of <fragment>.

如果你再要添加你已经创建其他一些布局(如main.xml中),您可以使用片段&LT;片断&gt; 引用它。例如,可以main.xml中改写为:

If you then want to add the fragment you've created to some other layout (such as main.xml), you can use <fragment> to reference it. For instance, main.xml could be rewritten as:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/details"
        android:name="com.qstudios.whatsopenlate.PlaceDetailsFragment"
        android:tag="detailsTag" />

</FrameLayout>

在这种情况下,你可以从main.java删除以下部分:

In which case you could remove the following part from main.java:

FragmentTransaction xTransaction = getSupportFragmentManager().beginTransaction();
PlaceDetailsFragment placeDetailsFragment = new PlaceDetailsFragment();
xTransaction.add(R.id.fragment_container, placeDetailsFragment);
xTransaction.commit();

不过,如果你选择使用FragmentManager编程添加片段(你有),就没有必要使用标签都没有。

If, however, you choose to add the fragment programatically with the FragmentManager (as you have), there's no need to use the tag at all.

这是在片段 API指南的所有解释得非常好(专门增加一个片段的活动),我会强烈建议阅读的部分。

This is all explained very well in the Fragments API Guide (specifically the part about adding a fragment to an activity), which I'd strongly suggest reading.

这篇关于&QUOT;重复ID,标签空,或父ID为0x0与另一个片段QUOT;添加第二个片段ActionBarActivity时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆