简体   繁体   English

为什么我不能在布局包含片段的情况下为布局充气?

[英]Why I can't inflate a layout where the layout contains a fragment?

I want to have a PopupWindow with a layout that contains a fragment but I can't inflate the layout My code is: 我想要一个PopupWindow,其布局包含一个片段,但是我无法为该布局充气我的代码是:

      View viewInsertPoints = getLayoutInflater().inflate(R.layout.insert_points, null);
      PopupWindow popupWindow = new PopupWindow(viewInsertPonts,null);

where insert_points.xml is: 其中insert_points.xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/row"
android:orientation="vertical">

<TableRow
    android:id="@+id/footer_table_row_4_jucatori_in_echipa_popup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padding_of_16"
    android:layout_weight="0"
    android:gravity="center"
    android:padding="@dimen/spazio_tra_celle">

    <com.ionvaranita.belotenote.borders.BorderedEditText
        android:id="@+id/puncte_noi_edit_text_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:inputType="number"
        android:maxLength="3"
        android:textAlignment="center"
        android:textColor="#000080"
        android:textSize="@dimen/footer_text_size"
        android:textStyle="bold" />

    <com.ionvaranita.belotenote.borders.BorderedEditText
        android:id="@+id/puncte_joaca_edit_text_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"

        android:gravity="right"
        android:inputType="number"
        android:maxLength="3"
        android:textAlignment="center"
        android:textSize="@dimen/footer_text_size" />

    <com.ionvaranita.belotenote.borders.BorderedEditText
        android:id="@+id/puncte_voi_edit_text_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="right"
        android:inputType="number"
        android:maxLength="3"
        android:textAlignment="center"
        android:textColor="#006400"
        android:textSize="@dimen/footer_text_size"
        android:textStyle="bold" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <TextView
        android:id="@+id/calculator_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/note"
        android:textAlignment="center"
        android:textSize="12sp" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <com.ionvaranita.belotenote.KeyboardFragment
        android:id="@+id/container_keyboard_4_jucatori_in_echipa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/padding_of_16"
        android:layout_marginTop="@dimen/padding_of_16"
        android:layout_weight="1" />

</TableRow>


</LinearLayout>

where the KeyboardFragment is a fragment that contains a personalised simple keyboard. 其中KeyboardFragment是包含个性化简单键盘的片段。 The keys of the keyboard are buttons with values, when I click the button it will write the value on the selected BorderedEditText object of the insert_points.xml layout 键盘上的键是带有值的按钮,当我单击该按钮时,它将值插入到insert_points.xml布局的选定BorderedEditText对象上

and the error: 和错误:

19 09:11:36.045 13393-13393/com.ionvaranita.belotenote E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.ionvaranita.belotenote, PID: 13393
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ionvaranita.belotenote/com.ionvaranita.belotenote.Tabella4JucatoriInEchipa}: android.view.InflateException: Binary XML file line #79: Binary XML file line #79: Class is not a View com.ionvaranita.belotenote.KeyboardFragment
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                            at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                         Caused by: android.view.InflateException: Binary XML file line #79: Binary XML file line #79: Class is not a View com.ionvaranita.belotenote.KeyboardFragment
                                                                         Caused by: android.view.InflateException: Binary XML file line #79: Class is not a View com.ionvaranita.belotenote.KeyboardFragment
                                                                         Caused by: java.lang.ClassCastException: class com.ionvaranita.belotenote.KeyboardFragment cannot be cast to android.view.View
                                                                            at java.lang.Class.asSubclass(Class.java:2469)
                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:606)
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
                                                                            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                            at com.ionvaranita.belotenote.Tabella4JucatoriInEchipa.onCreate(Tabella4JucatoriInEchipa.java:92)
                                                                            at android.app.Activity.performCreate(Activity.java:7009)
                                                                            at android.app.Activity.performCreate(Activity.java:7000)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                            at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                            at android.os.Looper.loop(Looper.java:164)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Can you tell me please an alternative of my intent? 你能告诉我替代我的意图吗?

This is the issue: 这是问题:

java.lang.ClassCastException: class com.ionvaranita.belotenote.KeyboardFragment cannot be cast to android.view.View

You need to declare your Fragment as such in the layout file, try this: 您需要在布局文件中这样声明您的Fragment ,请尝试以下操作:

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <fragment 
        android:name="com.ionvaranita.belotenote.KeyboardFragment"
        android:id="@+id/container_keyboard_4_jucatori_in_echipa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/padding_of_16"
        android:layout_marginTop="@dimen/padding_of_16"
        android:layout_weight="1" />

</TableRow>

If you leave out the fragment bit in the xml, Android will expect the class of KeyboardFragment to extend View ... 如果您省略xml中的fragment位,Android将期望KeyboardFragment类扩展View ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM