Reputation: 51
I am developing an application in Android and have a problem. I have two classes A and B. Their code is as below(for example):
public class A extends View {
//declaration of constructors
...
public class B extends Button {
//declaration of constructors
}
}
I try to add in xml the designer a class B, but I receive an error and it adds nothing.
<com.example.myproject.A.B
android:is="@+id/B1"
android:layout_wight="100dp"
android:layout_height="100dp"
.../>
What is the probable error and how to make a class as shown above free of errors in the designer of xml ?
Upvotes: 0
Views: 242
Reputation: 54705
Your code should be like this:
public class A extends View {
//declaration of constructors
...
public static class B extends Button {
//declaration of constructors
}
}
And the layout:
<view class="com.example.myproject.A$B"
android:id="@+id/B1"
android:layout_wight="100dp"
android:layout_height="100dp"
.../>
The most important thing here is that B
is static
nested class, not just inner class. You can't instantiate an inner class if you don't have an instance of an outer class, which is A
in your case.
Upvotes: 1
Reputation: 87064
You can't use those classes as creating an instance of B
would require a reference to A
in the first place. You either make the B
class static
in A
and use:
<view
class="com.example.myproject.A$B"
android:is="@+id/B1"
android:layout_wight="100dp"
android:layout_height="100dp"
/>
or you rethink your current class hierarchy.
Upvotes: 1