Search code examples
androiduser-interfaceandroid-custom-viewinflate-exception

Error inflating my Custom TextView from Library


I have customTextView that loads from my custom Library. but it can't inflate in main_layout and my app stops working. I find that If I put MyTextView in main project it works fine and the problem is load from 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();
    }

I get this warning in xml designing: the following classes could not be found on my custom class , although the class loads Auto completely! so I think some thing wrong with library. I create library in android studio by Project Strucure / new module / android library

this is how I call it:

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

and this is the error:

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)

Solution

  • 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");
        }
    }
    

    And in your layout

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