从库

时间:2015-04-29 10:12:13

标签: 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中创建库 Project Strucure / new module / android library

这就是我所说的:

   <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)

4 个答案:

答案 0 :(得分: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");
    }
}

并在你的布局中

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

答案 1 :(得分:0)

我只是删除被排除的可能错误......

修改

下一个可能的错误,你的TypedArray:

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

但你的styable被称为MyAttrs,所以它应该是:

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

但你必须获得你的属性,如下:

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

答案 2 :(得分:0)

确保您必须在build.gradle文件中定义必要的资源/资产

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

在我的情况下,我错过了资产声明

答案 3 :(得分:0)

这是因为应用程序和库包名称冲突。你必须改变其中一个。