Android 在我的案例中使用库项目的自定义视图

Android 在我的案例中使用库项目的自定义视图,android,android-layout,android-custom-view,android-library,android-custom-attributes,Android,Android Layout,Android Custom View,Android Library,Android Custom Attributes,我制作了一个安卓主库项目,其中我创建了一个自定义视图类: package com.my.android.library.layout; public class CustomView extends View { ... 我还为我的CustomView定义了styleable: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomView"&

我制作了一个安卓主库项目,其中我创建了一个自定义视图类:

package com.my.android.library.layout;

public class CustomView extends View {
...
我还为我的
CustomView
定义了styleable

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="CustomView">
        <attr name="title_text" format="string" />
    </declare-styleable>
</resources>
当我运行应用程序时,出现以下异常:

E/AndroidRuntime(30993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.android.app/com.my.android.app.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.my.android.library.layout.CustomView
E/AndroidRuntime(30993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
E/AndroidRuntime(30993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
    ...
   Caused by: java.lang.ClassNotFoundException: com.my.android.library.layout.CustomView
E/AndroidRuntime( 2947):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime( 2947):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime( 2947):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime( 2947):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
E/AndroidRuntime( 2947):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
似乎它无法在布局xml中膨胀my
CustomView
。为什么?如何摆脱它


(我检查了主库jar文件,有
CustomView
类。请不要在没有解释的情况下给我一个Android网站的链接。)

让我们这样做吧:

  public CustomView(Context context) {
    this(context, null, 0);
  }

  public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    //init your stuff here
  }

你能给我们详细的异常情况吗

因为您是从jar添加视图,所以应该能够将名称空间指定为:

 xmlns:custom="http://schemas.android.com/apk/lib/root_package_name
其中“root\u package\u name”可以是“com.my.android.library.layout”或“com.my.android.library”之类的

如果这不起作用,那么很可能是“订单和导出”问题,或者是添加库的方式。确保正确包含库:

最后,如果同样失败,那么您可能需要更新Android SDK:


我说我正在使用分布式库项目,这意味着我在我的应用程序项目中引用了库项目。为什么要否决我今年5月阅读的帖子help@Raghunandan,我看不到此链接回答了我的问题。在CustomView类中是否有接受3个参数的构造函数?你们有三个不同的构造器吗?
 xmlns:custom="http://schemas.android.com/apk/lib/root_package_name