【问题标题】:App getting force close when the fragment opened for second time片段第二次打开时应用程序强制关闭
【发布时间】:2015-02-02 12:39:53
【问题描述】:

我从 xml 文件创建一个片段,并在活动中的 setcontentview 方法中直接添加 xml 文件。如果我重新打开该片段导致强制关闭,它第一次工作正常。

错误日志中的问题是

02-02 18:09:22.323: E/AndroidRuntime(5591): FATAL EXCEPTION: main
02-02 18:09:22.323: E/AndroidRuntime(5591): Process: com.pal.newloginscreenui, PID: 5591
02-02 18:09:22.323: E/AndroidRuntime(5591): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at com.pal.newloginscreenui.DashboardActivity.display_popopMenu(DashboardActivity.java:112)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at com.pal.newloginscreenui.DashboardActivity.onClick(DashboardActivity.java:80)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.View.performClick(View.java:4463)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.View$PerformClick.run(View.java:18770)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.os.Handler.handleCallback(Handler.java:808)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.os.Handler.dispatchMessage(Handler.java:103)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.os.Looper.loop(Looper.java:193)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.app.ActivityThread.main(ActivityThread.java:5292)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at java.lang.reflect.Method.invoke(Method.java:515)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at dalvik.system.NativeStart.main(Native Method)
02-02 18:09:22.323: E/AndroidRuntime(5591): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f06004b, tag null, or parent id 0xffffffff with another fragment for com.pal.newloginscreenui.NavigationMenuFragment
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.app.Activity.onCreateView(Activity.java:4824)
02-02 18:09:22.323: E/AndroidRuntime(5591):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-02 18:09:22.323: E/AndroidRuntime(5591):     ... 17 more

xml:

<FrameLayout android:id="@+id/containner"
   android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:id="@+id/frag"
   android:name="com.pal.newloginscreenui.NavigationMenuFragment" 
   android:layout_width="match_parent"
   android:layout_height="match_parent"/> 
</FrameLayout>

我正在调用的弹出窗口

DoctorGlobal.pw = new PopupWindow(DashboardActivity.this);// create a popup window
    DoctorGlobal.pw.setTouchable(true);
    DoctorGlobal.pw.setFocusable(true);
    DoctorGlobal.pw.setOutsideTouchable(true);
    DoctorGlobal.pw.setTouchInterceptor(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { 
                DoctorGlobal.pw.dismiss();
                return true;
            }
            return false;
        }
    });

    int popWndwidth = (int)(width/4)*3;
    DoctorGlobal.pw
            .setWidth(popWndwidth);
    DoctorGlobal.pw
            .setHeight(android.view.ViewGroup.LayoutParams.MATCH_PARENT);
    View contentView = getLayoutInflater().inflate(R.layout.navg_menu, null);
    DoctorGlobal.pw.setContentView(contentView);
    container = (FrameLayout) contentView.findViewById(R.id.containner);
    DoctorGlobal.pw.showAsDropDown(v, 0, 0);

navg_menu中具有上述xml代码

【问题讨论】:

  • 请同时分享布局xml。你在使用子片段吗? (例如片段中的片段)
  • 没有一个片段

标签: android android-layout android-fragments


【解决方案1】:

你的错误:

android.view.InflateException: Binary XML file line #7: Error inflating class fragment

  ....

Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: 
Duplicate id 0x7f06004b, tag null, or parent id 0xffffffff with another 
fragment for com.pal.newloginscreenui.NavigationMenuFragment

你的 xml 标签:

<FrameLayout android:id="@+id/containner"
  ....                                      >
<fragment android:id="@+id/frag"
    android:name="com.pal.newloginscreenui.NavigationMenuFragment" 
  ....

你的java:

View contentView = getLayoutInflater().inflate(R.layout.navg_menu, null);

你有getLayoutInflater().inflate(R.layout.navg_menu, null);

它会出现navg_menu id 不存在或者它是您试图膨胀的菜单 xml,也许应该用于菜单并确保您有一个正确的 id 来创建您的 contentView。;

`MenuInflater inflater = getMenuInflater();`

`getMenuInflater().inflate`

或者您正在访问的 id 重复。 希望这会帮助您调试它。

我认为这里的答案会对您有所帮助:
https://stackoverflow.com/a/8513735/3956566

或在您完成片段后将其关闭,如下所示:

https://stackoverflow.com/a/14966061/3956566

【讨论】:

  • 没有重复的ID。我在想问题是我第一次调用片段两次没有问题,因为在片段后台堆栈中没有片段,我第二次打开它抛出问题可能是片段在后台堆栈中。跨度>
  • 其实是在xml文件中声明的。所以我没有在 java 代码中处理任何东西。
  • 是的,上面的示例通过删除片段形式片段事务来工作。现在工作正常。谢谢sss
猜你喜欢
  • 2021-09-25
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
相关资源
最近更新 更多