android - 在我的案例中使用库项目中的自定义 View

标签 android android-layout android-custom-view android-library android-custom-attributes

我做了一个 Android 主库项目,我在其中创建了一个自定义 View 类:

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>

因为我不想把我的源代码分享给其他正在使用我的库项目的人,所以我创建了一个分布式库项目,在其中我添加了上面ma​​in的jar library projectdistributed library projectlibs/ 文件夹并将所有资源从ma​​in library project 复制到distributed library project .

接下来,我制作了一个使用分布式库项目android 应用程序项目。在应用项目的主布局文件中,我定义了以下内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <com.my.android.library.layout.CustomView
        custom:title_text = "THIS IS TITLE" 
        />
<RelativeLayout>

当我运行我的应用程序时,出现以下异常:

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 中膨胀我的 CustomView。为什么?如何摆脱它?

(我查看了主库jar文件,里面有CustomView类,请不要不解释就直接给我一个Android网站的链接。)

最佳答案

由于您是从 jar 添加 View ,因此您应该能够将命名空间指定为:

 xmlns:custom="http://schemas.android.com/apk/lib/root_package_name

其中“root_package_name”可以是“com.my.android.library.layout”或“com.my.android.library”或类似的东西。

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

Android Activity ClassNotFoundException - tried everything

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

ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView"

关于android - 在我的案例中使用库项目中的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25180489/

相关文章:

java - android应用程序关闭时如何保持最后一种语言?

android - 如何通过样式自定义操作栏上的后退按钮

java - 从 onClickListener 类显示 Toast 的上下文

android - 如何从android中的画廊上传图片

android - 以编程方式将自定义 View 放入 ScrollView(或 Horizo​​ntalScrollView)(无滚动)

android - 自定义 Android 日历 View

android - 使用 obtainStyledAttributes 获取多个样式属性

android - 如何调用来自不同 Activity 的 View 寻呼机中的 fragment ?

android - 服务中的 sleep 功能是否会影响android启动时间

android - 为什么自定义首选项的字体更大?