[英]Why I can't inflate a layout where the layout contains a fragment?
我想要一個PopupWindow,其布局包含一個片段,但是我無法為該布局充氣我的代碼是:
View viewInsertPoints = getLayoutInflater().inflate(R.layout.insert_points, null);
PopupWindow popupWindow = new PopupWindow(viewInsertPonts,null);
其中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>
其中KeyboardFragment是包含個性化簡單鍵盤的片段。 鍵盤上的鍵是帶有值的按鈕,當我單擊該按鈕時,它將值插入到insert_points.xml布局的選定BorderedEditText對象上
和錯誤:
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)
你能告訴我替代我的意圖嗎?
這是問題:
java.lang.ClassCastException: class com.ionvaranita.belotenote.KeyboardFragment cannot be cast to android.view.View
您需要在布局文件中這樣聲明您的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>
如果您省略xml中的fragment
位,Android將期望KeyboardFragment
類擴展View
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.