android - 从库中扩充我的自定义 TextView 时出错

标签 android user-interface android-custom-view inflate-exception

我有从我的自定义库中加载的 customTextView。但它无法在 main_layout 中膨胀,我的应用程序停止工作。我发现如果我将 MyTextView 放在主项目中,它工作正常,问题是从 myLib 加载。

public class MyTextView extends TextView {


    public MyTextView(Context context){
    super(context);

}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    initAttr(attrs);
}

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    initAttr(attrs);
}


    private void initAttr(AttributeSet attrs) {
        TypedArray a=getContext().obtainStyledAttributes(
                attrs,
                R.styleable.myView);

        super.setText("blablabla");



        a.recycle();
    }

我在 xml 设计中收到此警告:在我的自定义类上找不到以下类,尽管该类已完全加载 Auto!所以我认为图书馆有些问题。我在 android studio 中创建库 项目结构/新模块/android库

我是这样调用它的:

   <com.kenji.mylib.MyTextView
        android:layout_width="50dp"
        android:layout_height="50dp"/>

这是错误:

04-29 14:22:35.645  19805-19805/com.kenji.myapplication E/AndroidRuntime? FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kenji.myapplication/com.kenji.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.kenji.mylib.myTextView.myTextView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
            at android.app.ActivityThread.access$1500(ActivityThread.java:121)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3701)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.kenji.mylib.myTextView.myTextView
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)

最佳答案

public class MyTextView extends TextView
{
    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init();
    }
    public MyTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }
    public MyTextView(Context context)
    {
        super(context);
        init();
    }
    private void init()
    {
        setText("blablabla");
    }
}

在你的布局中

<yourpackage.MyTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

关于android - 从库中扩充我的自定义 TextView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29940661/

相关文章:

android - ListView 的第一个条目对于 RTL 总是不正确

android - 我们可以将触摸板或触控板值模拟到 android 平板电脑吗?

android - 根据构建类型通过 Gradle 设置 Android.mk 标志

user-interface - 如何将组件添加到指南创建的现有 GUI 中?

Ruby:Linux ubuntu 上的图形用户界面 tk 不起作用

android - 自定义EditText settext不设置文本

android - android studio 中图形的硬件和软件 GLES 2.0 选项是什么?

java - 为什么每次执行 ActionListener 时我的 GUI 窗口都会卡住?

java - 如何从屏幕右侧开始将 `ListView` 项包装到wrap_content?

android - 在 Android 中使用 Path.arcTo() 绘制环的奇怪行为