簡體   English   中英

Android:擴展LinearLayout的自定義復合類認為這是LinearLayout

[英]Android: Custom composite class extending LinearLayout thinks it's a LinearLayout

我正在創建一個自定義復合布局,該布局由可單擊的水平LinearLayout組成,其中包含ImageView和兩個TextViews。 最終,我希望能夠使用單個字段引用整個內容,並根據用戶活動將其添加,減去和編輯到我的活動中。 我正在使用在構造函數中擴展的XML資源。 但是我的電話認為我正在創建LinearLayout,並且在嘗試實現自定義類時收到以下錯誤:

D/ConnectionButton﹕ Ready to inflate
D/ConnectionButton﹕ constructor 2
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417be898)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampleapp.app/com.exampleapp.app.ManageFriends}: android.view.InflateException: Binary XML file line #3: Error inflating class com.exampleapp.utils.ConnectionButton

我設置NameText時的錯誤。 如果我把那行留在外面,它會運行,但是我以后的日志告訴我,android認為它是LinearLayout,而不是ConnectionButton

所以這是我的定義類ConnectionButton:

public class ConnectionButton extends LinearLayout {
    ImageView IconView;
    TextView  NameView;
    TextView  StatusView;

    public ConnectionButton(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.d("ConnectionButton","Ready to inflate");
        addView(inflater.inflate(R.layout.connection_row, null));
        Log.d("ConnectionButton","Inflated");
        IconView = (ImageView) findViewById(R.id.icon);
        NameView = (TextView) findViewById(R.id.name);
        StatusView = (TextView) findViewById(R.id.status);
    }

    public ConnectionButton (Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d("ConnectionButton","constructor 2");
/*        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.d("ConnectionButton","Ready to inflate");
        addView(inflater.inflate(R.layout.connection_row, null));
        Log.d("ConnectionButton","Inflated");*/
        IconView = (ImageView) this.findViewById(R.id.icon);
        NameView = (TextView) this.findViewById(R.id.name);
        StatusView = (TextView) this.findViewById(R.id.status);
        NameView.setText("From Constructor 2");
    }
}

這是我的XML資源:

<com.exampleapp.utils.ConnectionButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:clickable="true"
    style="@android:style/Widget.Button">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:adjustViewBounds="true"
            android:paddingRight="5dp"
            android:id="@+id/icon"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:layout_weight="2"
            android:id="@+id/name"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:layout_weight="1"
            android:id="@+id/status"/>
</com.exampleapp.utils.ConnectionButton>

這是我在其中創建它的地方:(注意,NB ConnectionList是應該包含這些對象的現有垂直LinearLayout)

    LinearLayout box = (LinearLayout)findViewById(R.id.ConnectionList);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Log.d("ConnectionButton","Ready to inflate");
    View CBv = inflater.inflate(R.layout.connection_row, box);
    Log.d("CBv class",CBv.getClass().toString());

您錯誤地假設inflater.inflate返回新視圖。 根據文檔,它返回:

膨脹層次結構的根視圖。 如果提供了root,則這是根視圖; 否則,它是膨脹的XML文件的根。

因此,在您的情況下,CBv是LinearLayout,因為這就是父級。 要訪問您的視圖,只需使用findViewById。

我認為您正在嘗試在ConnectionButton的構造函數中ConnectionButton ,這帶來了遞歸問題。

我建議您更改ConnectionButton,使其改為繼承自FrameLayout (因為您只有一個根孩子),然后將其XML布局內容的根類型更改為LinearLayout 然后,您的控件將是包含單個LinearLayout的擴展FrameLayout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM