简体   繁体   中英

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:

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

where insert_points.xml is:

<?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. 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

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:

<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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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