In an Android application, I have created a custom view extending the View
class. I want to declare an object of my class in the xml layout file but, even after many tries, the application unexpectedly stops when the setContentView
call is executed (that's what the popup windows which appears says).
MainActivity
file as public, I have two constructors : one with only the Context
as parameter and one with a Context
and an AttributeSet
parameters. And I have overridden the onDraw
function.
This class is in my source package, named org.me.myapp
.
In the layout file, I declar开发者_如何学Goe the object I want to insert like this :
<org.me.myApp.MainActivity.myView"
android:id="@+id/View"
android:layout_below="@id/toto"
android:layout_width="300dip"
android:layout_height="100dip"/>
Can anyone tell me what is wrong?
Thanks in advance for the time you will spend trying to help me.
What Yar said, you should extend View or ViewGroup to create custom layout element, and use that class name in your layout xml. Not member variable what it looks you're trying to do. After inflating your layout you can access your custom view with;
org.me.myapp.MyView myView = (org.me.myapp.MyView) findViewById(R.id.MYVIEWID);
I think you need to declare your custom layout as a separate class, for example: org.me.myapp.MyView and relate to it in your xml file like this:
<org.me.myapp.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
...
/>
精彩评论