1

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)
Mahdi
  • 6,139
  • 9
  • 57
  • 109

4 Answers4

3
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" />
Harin
  • 2,413
  • 3
  • 15
  • 30
0

I just delete the excluded possible mistakes...

EDIT

next possible mistake, Your TypedArray:

  TypedArray a=getContext().obtainStyledAttributes(
            attrs,
            R.styleable.myView);

But Your stylable is called MyAttrs, so it should be:

  TypedArray a=getContext().obtainStyledAttributes(
            attrs,
            R.styleable.MyAttrs);

But You have to obtain Your attributes, like this:

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

    TypedArray a=context.obtainStyledAttributes(
                attrs,
                R.styleable.MyAttrs);
}

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

    TypedArray a=context.obtainStyledAttributes(
                attrs,
                R.styleable.MyAttrs);
}
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
0

Make sure u must define necessary resources/assets in build.gradle file

  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      java.srcDirs = ['src']
      res.srcDirs = ['res']
      assets.srcDirs = ['assets']
    }

In my case i was missing the declaration for assets

0

It's because that the app and library package name clashes. you must change one of them.

Shervin Gharib
  • 728
  • 2
  • 14
  • 29