显示弹出窗口内的片段

时间:2014-01-23 17:01:55

标签: android android-fragments

我正在弹出窗口中显示片段。但是我在这方面表现出错。我用谷歌搜索解决方案。但仍然没有解决这个问题。请帮我。我是android的新手。我的代码是

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View popUp = getLayoutInflater().inflate(R.layout.popup_item, null);
        window = new PopupWindow(getApplicationContext());
        window = new PopupWindow(popUp,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,true);
        window.setAnimationStyle(android.R.style.Animation_Activity);
        window.showAtLocation(popUp, Gravity.BOTTOM, 0, 0);
        window.setFocusable(true);
        window.setOutsideTouchable(true);
    }

popupitem.xml //用于在MainActivity中显示片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/popup_fragment"
        android:name="com.example.popupemoji.PopupFragment"
        android:tag=""/>
</LinearLayout>

我有一个扩展Fragment的类,在onCreateView上只有两行代码

View view = inflater.inflate(R.layout.popup_item, container);
        return view;

我在任何地方都错了。 Logcat错误是

01-23 14:25:24.005: E/AndroidRuntime(2868): FATAL EXCEPTION: main
01-23 14:25:24.005: E/AndroidRuntime(2868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.popupemoji/com.example.popupemoji.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.os.Looper.loop(Looper.java:137)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at dalvik.system.NativeStart.main(Native Method)
01-23 14:25:24.005: E/AndroidRuntime(2868): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at com.example.popupemoji.PopupFragment.onCreateView(PopupFragment.java:15)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at com.example.popupemoji.MainActivity.onCreate(MainActivity.java:18)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.Activity.performCreate(Activity.java:5104)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-23 14:25:24.005: E/AndroidRuntime(2868):     ... 11 more
01-23 14:25:24.005: E/AndroidRuntime(2868): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f080002, tag , or parent id 0x0 with another fragment for com.example.popupemoji.PopupFragment
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
01-23 14:25:24.005: E/AndroidRuntime(2868):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

1 个答案:

答案 0 :(得分:1)

如果我正确阅读您的代码,您将通过膨胀popup_item.xml布局来创建View。此布局包含一个Fragment,它也会使popup_item.xml膨胀。

因此,Fragment在popup_item.xml布局中也会有另一个Fragment,这将继续无限期地继续。

您的错误显示应用​​程序因此崩溃:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: 
    Duplicate id 0x7f080002, tag , or parent id 0x0 with another fragment 
    for com.example.popupemoji.PopupFragment

由于这个popup_item.xml嵌套,多个视图具有相同的ID,这是一个问题。即使多个视图具有相同的ID,您也会有一个无限嵌套的片段,这也是一个问题。