Error related to inner class View in layout / main.xml

Hmm ...

I am subclassing the view as an inner class in my work. Before I just get attached to this view from my work, do the following:

setContentView(new CustomView(this)); 

no problem.

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

 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 until when I directly contacted it, but when I tried to add it to the main.xml file as part of the framework, I got the above error. I also tried to insert the layout, only displaying it through:

 <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 InflateException / ClassNotFoundException errors. He complains about "line number 3" in the binary XML file, and if it comes to main.xml, which is a package declaration that I checked three times.

EDIT I tried to make this view a separate class (i.e. Not an inner class) and it works. After some searching, I found a few posts saying that the xml tag should look like this:

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

That is, the dollar sign should be used for the branch of the inner class from the main class. However, Eclipse barfs on this, calls it a bug and refuses to let me create or deploy with this symbol. So now the question is: how does the view, which is an inner class, refer to?

+52
android layout view
Jan 20 '10 at 0:44
source share
6 answers

For inner classes, the syntax will look like this:

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

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

+145
Jan 20 '10 at 9:01
source share

I had the same problem. However, the syntax in the XML file was correct.

As a result, I decided that the inner class should be declared as static. For example:

 public static class myWebView extends WebView 
+36
Jun 04 2018-11-11T00:
source share

for inner class:

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

and for the inner class you have to declare your class:

 public static InnerClass 



static

+11
Mar 13 '15 at 13:57
source share
 <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 forgot some elements, such as layout_width, my program crashed. I also had to make my class class static in order for it to work. In the end, it would be the same if I just pulled it out of the socket. The android note example uses a nested class.

+2
Apr 27 2018-11-11T00:
source share

You need to specify the full class name of the view in XML for inflation to work, and the View Class to be found in the Runtime System.
Since you declared your View as the inner class of your activity, the full name will be: <your_package_name>.OuterClassName.InnerClassName

Are you sure com.grafightscratch.ochemmer.CustomView is the full name of your class?

EDIT: Thanks for reminding me of this. When views are declared as nested classes, there is a slight aberration; see Use the Custom Component of this document.

0
Jan 20 '10 at 4:41
source share

Here are some key points for creating a custom view inside an inner class ...

 public static class MainClass { .... .... public class SubClassView extends LinearLayout { public SubClassView(Context context, AttributeSet attrs) { super(context, attrs); ..... } .... .... } } 

The layout should be as follows:

 <view class = ".MainClass$SubClassView" android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/button"/> 

Java class

  • static required
  • constructor with AttributeSet required (at least one)

Xml file

  • Tag
  • view (lower case NOT View) required
  • class indicating the path to your inner class using
  • $ instead of "." before the name SubClassView
0
Sep 14 '17 at 15:48
source share



All Articles