【问题标题】:How to add inherited class to XML layout in android如何在android中将继承的类添加到XML布局
【发布时间】:2014-02-05 01:26:43
【问题描述】:

我正在关注此链接: android spinner fire event when same item selection is made

我扩展了链接中提到的Spinner 类。以下是我的代码:

    public class SpinnerTrigger extends Spinner {

    public SpinnerTrigger(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    }

    @Override
public void setSelection(int position, boolean animate) {
    ignoreOldSelectionByReflection();
    super.setSelection(position, animate);
}

private void ignoreOldSelectionByReflection() {
        try {
        Class<?> c = this.getClass().getSuperclass().getSuperclass().getSuperclass();
        Field reqField = c.getDeclaredField("mOldSelectedPosition");
        reqField.setAccessible(true);
        reqField.setInt(this, -1);
    } catch (Exception e) {
        Log.d("Exception Private", "ex", e);
        // TODO: handle exception
    }
}

@Override
public void setSelection(int position) {
    ignoreOldSelectionByReflection();
    super.setSelection(position);
}

}

我在我的布局文件中添加了上面的类,具有完全限定的名称:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <com.mypack.cc.SpinnerTrigger
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/options"
        android:prompt="@string/text_prompt" />

    </LinearLayout>

当我尝试运行我的应用程序时,我收到如下所述的错误日志:

Caused by: android.view.InflateException: Binary XML file line #12:Error inflating class com.mypack.cc.SpinnerTrigger

如果我在这里错过了什么,请给我建议。

【问题讨论】:

  • 请包含错误的整个堆栈跟踪

标签: android


【解决方案1】:

将此添加到您的布局中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:tag="http://schemas.android.com/apk/res/com.mypack.cc"  <!--   HERE -->

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 2021-03-28
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多