错误充气片段 [英] Error inflating fragment

查看:108
本文介绍了错误充气片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想跟着一起在Android开发的例子code膨胀片段中的活动。我有一个更复杂的项目我的工作,但我得到了同样的错误,如下这个简单的问题。任何人都可以点我在正确的方向?

扩展片段类

 包com.example.fragtest;


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

公共类FragOne扩展片段{

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
    }
    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.activity_main,集装箱,假);
    }

}
 

扩展FragmentActivity类

 包com.example.fragtest;

进口android.os.Bundle;
进口android.view.Menu;
进口android.support.v4.app.FragmentActivity;

公共类MainActivity扩展FragmentActivity {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }



}
 

在XML的片段是

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

   <片段机器人:名称=com.example.fragtest.FragOne
       机器人:ID =@ + ID / FRAG
       机器人:layout_width =match_parent
       机器人:layout_height =match_parent/>

< / LinearLayout中>
 

在这里活动的名称设置的清单

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.fragtest
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =11
        机器人:targetSdkVersion =15/>

    <应用
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=。MainActivity
            机器人:标签=@字符串/ title_activity_main>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

下面是logcat的输出

  07-17 23:45:09.053:E / AndroidRuntime(560):致命异常:主要
07-17 23:45:09.053:E / AndroidRuntime(560):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.fragtest / com.example.fragtest.MainActivity}:android.view.InflateException:二进制XML文件行#7:错误充气类片段
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.ActivityThread.access $ 600(ActivityThread.java:122)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.os.Handler.dispatchMessage(Handler.java:99)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.os.Looper.loop(Looper.java:137)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.ActivityThread.main(ActivityThread.java:4340)
07-17 23:45:09.053:E / AndroidRuntime(560):在java.lang.reflect.Method.invokeNative(本机方法)
07-17 23:45:09.053:E / AndroidRuntime(560):在java.lang.reflect.Method.invoke(Method.java:511)
07-17 23:45:09.053:E / AndroidRuntime(560):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 23:45:09.053:E / AndroidRuntime(560):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 23:45:09.053:E / AndroidRuntime(560):在dalvik.system.NativeStart.main(本机方法)
07-17 23:45:09.053:E / AndroidRuntime(560):android.view.InflateException:二进制XML文件中的行#7:错误充气类片段产生的原因
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 23:45:09.053:E / AndroidRuntime(560):在com.example.fragtest.FragOne.onCreateView(FragOne.java:18)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 23:45:09.053:E / AndroidRuntime(560):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.Activity.setContentView(Activity.java:1835)
07-17 23:45:09.053:E / AndroidRuntime(560):在com.example.fragtest.MainActivity.onCreate(MainActivity.java:12)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.Activity.performCreate(Activity.java:4465)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-17 23:45:09.053:E / AndroidRuntime(560):11 ...更多
07-17 23:45:09.053:E / AndroidRuntime(560):与另一个片段com.example重复ID 0x7f080000,标记为空,或父ID为0x0:致:java.lang.IllegalArgumentException:如果二进制XML文件中的行#7 .fragtest.FragOne
07-17 23:45:09.053:E / AndroidRuntime(560):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
07-17 23:45:09.053:E / AndroidRuntime(560):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 23:45:09.053:E / AndroidRuntime(560):30 ...更多
 

解决方案

在你的类,它扩展 FragmentActivity 要设置视图标题为XML文件 activity_main 。在 activity_main 你有一个关于你的片段的code, com.example.fragtest.FragOne 。 当这是在加载XML时,code与 com.example.fragtest.FragOne 执行有关。这的片段的的code返回的查看的与 R.id.activity_main

这是XML文件中引用了相同的片段的$ C $那个刚刚实例化它℃。你看的难题。

要解决你的问题,比如一个新的XML文件(一个未命名的 activity_main )在你的片段的类。相反充气 activity_main 的膨胀单独的XML(一个不包含引用code调用它)。有效 activity_main 应在容器中,这个新的布局(比如 fragment_layout )将是UI部分。

我喜欢把它像这样:尽管主义,一个的片段的有三个部分。容器的活动的(这可以了,你的情况应该夸大通过的setContentView布局XML文件),在片段的的Java code(这个就可以了,你的情况应该夸大布局....比 activity_main ),而片段的的相关XML文件(其中一些其他的通过您的片段级膨胀,不包括循环引用回到它的code)。

有,当然,漏洞,这些咒语,但你的意思吗?

I'm just trying to follow along with the example code on android dev to inflate a fragment in an activity. I have a more complicated project I'm working but I'm getting the same error as this simple one here. Can anyone point me in the right direction?

The class that extends fragment

package com.example.fragtest;


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

public class FragOne extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.activity_main, container,false);
    }

}

The class that extends FragmentActivity

package com.example.fragtest;

import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



}

The xml where the fragment is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >

   <fragment android:name="com.example.fragtest.FragOne"
       android:id="@+id/frag"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</LinearLayout>

The manifest where the activity name is set

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fragtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here is the logcat output

07-17 23:45:09.053: E/AndroidRuntime(560): FATAL EXCEPTION: main
07-17 23:45:09.053: E/AndroidRuntime(560): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragtest/com.example.fragtest.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.os.Looper.loop(Looper.java:137)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.ActivityThread.main(ActivityThread.java:4340)
07-17 23:45:09.053: E/AndroidRuntime(560):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 23:45:09.053: E/AndroidRuntime(560):  at java.lang.reflect.Method.invoke(Method.java:511)
07-17 23:45:09.053: E/AndroidRuntime(560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 23:45:09.053: E/AndroidRuntime(560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 23:45:09.053: E/AndroidRuntime(560):  at dalvik.system.NativeStart.main(Native Method)
07-17 23:45:09.053: E/AndroidRuntime(560): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 23:45:09.053: E/AndroidRuntime(560):  at com.example.fragtest.FragOne.onCreateView(FragOne.java:18)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-17 23:45:09.053: E/AndroidRuntime(560):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.Activity.setContentView(Activity.java:1835)
07-17 23:45:09.053: E/AndroidRuntime(560):  at com.example.fragtest.MainActivity.onCreate(MainActivity.java:12)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.Activity.performCreate(Activity.java:4465)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-17 23:45:09.053: E/AndroidRuntime(560):  ... 11 more
07-17 23:45:09.053: E/AndroidRuntime(560): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragtest.FragOne
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
07-17 23:45:09.053: E/AndroidRuntime(560):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
07-17 23:45:09.053: E/AndroidRuntime(560):  ... 30 more

解决方案

In your Class that extends FragmentActivity you are setting the view to an XML file titled activity_main. Inside of activity_main you have a reference to your Fragment code, com.example.fragtest.FragOne. When that XML is loaded, the code associated with com.example.fragtest.FragOne is executed. That Fragment's code returns a View with the R.id.activity_main.

That XML file has a reference to the same Fragment code that had just instantiated it. You see the conundrum.

To resolve your issue instance a new XML file (one that is not named activity_main) in your Fragment class. Instead of inflating activity_main inflate a separate XML (one that does not contain a reference to the code that called it). Effectively activity_main should be the container, this new layout (for instance fragment_layout) will be the UI portion.

I like to think of it like this: Despite doctrine, a Fragment has three portions. The container Activity (this can, and in your case should, inflate a layout XML file via setContentView), the Fragment's java code (this can, and in your case should, inflate a layout....something other than activity_main), and that Fragment's associated XML file (the one that is inflated by your Fragment class, don't include a cyclical reference back to it's code).

There are, of course, loopholes to these mantras but you get the drift?

这篇关于错误充气片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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