Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use custom view from library project in my case

I have made an Android main library project, in which I have created a custom view class:

package com.my.android.library.layout;

public class CustomView extends View {
...

I also defined styleable for my CustomView:

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="CustomView">
        <attr name="title_text" format="string" />
    </declare-styleable>
</resources>

Since I don't want to share my source code to others who are using my library project, so I created an distributed library project, in which I added the jar of above main library project to libs/ folder of distributed library project and copied all resource from main library project to distributed library project.

NEXT, I have made an android app project which uses the distributed library project. In the main layout file of app project, I defined the following :

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

When I run my app, I got the following exception:

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)

It seems it can not inflate the my CustomView in layout xml. Why? How to get rid of it?

(I checked the main library jar file, there is CustomView class. Please don't just throw me a link of Android website without explanation.)

like image 563
Leem.fin Avatar asked Nov 23 '22 15:11

Leem.fin


1 Answers

Since you are adding the View from a jar, you should be able to specify the namespace as:

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

Where "root_package_name" could be "com.my.android.library.layout" or "com.my.android.library" or something like that.

If that doesn't work, then it's probably and "Order and Export" problem or the way you are adding the library. Make sure the library is included properly:

Android Activity ClassNotFoundException - tried everything

And finally, if that also fails, then you might need to update Android SDK:

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

like image 113
Jim Avatar answered Nov 25 '22 04:11

Jim