Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error referencing an inner class View in layout/main.xml

Grrr...

I create a subclass of view as an inner class in my Activity. Before I simply linked to this view from my activity with:

setContentView(new CustomView(this));

without problems.

Now, however, my view is getting more complex so I am making it part of a FrameLayout so that I can make this the base view and add a Spinner widget on top of it. The problem is, when I do this I get an error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grafightscratch.ochemmer/com.grafightscratch.ochemmer.MoleculeTablet}: android.view.InflateException: Binary XML file line #4: Error inflating class com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView
...
Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView
...
Caused by: java.lang.ClassNotFoundException: com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView in loader dalvik.system.PathClassLoader@43b74a28

So- this view worked before when I linked to it directly, but when I tried to add it in the main.xml file as part of a framelayout I got the above error. I also tried putting into a layout with only it being displayed via:

<com.grafightscratch.ochemmer.MoleculeTablet.MoleculeTabletView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/molecule_tablet_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Nothing works. I keep getting the InflateException/ClassNotFoundException errors. It complains about "line #3" in the binary XML file, and if it is talking about main.xml that is the package declaration which I have triple checked.

EDIT I tried making this view a separate class (ie- not an inner class) and it works. After some searching around I found some posts saying that the xml tag should look like this:

<com.grafightscratch.ochemmer.MoleculeTablet$MoleculeTabletView ...>

Ie, a dollar sign should be used to separate the innerclass from the main class. However, Eclipse barfs on this, calls it an error, and refuses to let me build or deploy with that character there. So now the question becomes: how does one reference a View that is an inner class?

like image 632
IcedDante Avatar asked Jan 20 '10 00:01

IcedDante


4 Answers

For inner classes the syntax becomes:

<view class="com.grafightscratch.ochemmer.MoleculeTablet$MoleculeTabletView" />

The reason is that $ is an illegal character in XML tags.

like image 190
Romain Guy Avatar answered Oct 19 '22 05:10

Romain Guy


I was having the same issue. The syntax in the XML file was correct, however.

What ended up resolving the issue for me was that the inner class needs to be declared as static. For example:

public static class myWebView extends WebView
like image 20
Whatzit Toya Avatar answered Oct 19 '22 07:10

Whatzit Toya


for inner class :

<view class="{package}.{ParentClass}${innerClass}" />

and for inner class , you must declare your class :

public static InnerClass

static is require .

like image 35
Adnan Abdollah Zaki Avatar answered Oct 19 '22 07:10

Adnan Abdollah Zaki


<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="com.example.Myproject.Myactivity$Myview"
     android:layout_width="fill_parent" android:id="@+id/name" android:visibility="visible" android:layout_gravity="bottom" android:layout_height="fill_parent" android:focusableInTouchMode="true"
/>

this code worked for me. When i left out some of the elements like layout_width my program crashed. I also had to make my view class static in order for it to work. In the end It would have been the same if i just took it out of its nest. The android note example uses a nested class.

like image 30
justin Avatar answered Oct 19 '22 06:10

justin