5

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

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • I said I am using distributed library project, which implies I have referenced the library project in my app project. – Leem.fin Aug 07 '14 at 10:49
  • reading this http://developer.android.com/tools/projects/index.html may help – Raghunandan Aug 07 '14 at 10:56
  • @Raghunandan , I don't see this link answers my question. – Leem.fin Aug 07 '14 at 10:59
  • Do you have a constructor accepting 3 parameters in your CustomView class? and Do you have three different constructors? – Illegal Argument Aug 07 '14 at 11:14
  • @IllegalArgument, no, I have only one constructor with Context & AttributeSet, this is what the Android Developer document says: http://developer.android.com/training/custom-views/create-view.html – Leem.fin Aug 07 '14 at 11:21
  • @Leem.fin after reading your post multiple times I think your library setup process. *com.my.android.app/com.my.android.app.MainActivity* this line makes me think so. If the package shown would be your library package *com.my.android.library.layout.CustomView* then the error would be different. My guess is that the code is searching for CustomView in com.my.android.app but I am not so sure. Ensure that your build paths are correct – Illegal Argument Aug 07 '14 at 11:40
  • @Leem.fin can you post an image of your `android private library ` and `android depedencies` in java build path – Rod_Algonquin Aug 11 '14 at 01:59
  • @Leem.fin add `android:layout_width` and `android:layout_height` to your `CustomView` and let me know the result. after that if `ClassNotFoundException` throws tell me. – mmlooloo Aug 16 '14 at 18:40

2 Answers2

1

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"

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
-2

Let's try do it this way:

  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
  }

Could you give us full stacktrace of the exception ?

Peter Moskala
  • 360
  • 1
  • 9