7

I created a class like this

public final class MyView extends View {

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        [...]
    }
        [...]
}

and then I want to use it within my layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent">

  <com.hitziger.barcode.MyView
      android:id="@+id/my_view"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>

</FrameLayout>

But Eclipse tells me in the error log

AndroidManifest: Ignoring unknown 'com.hitziger.barcode.MyView' XML element

How can I make MyView accessable within a layout? Do I have to publish this class elsewhere?

hitzi
  • 852
  • 2
  • 10
  • 23
  • can you try to rebuild your project and deploy it? Seems like eclipse is messed up... – Vladimir Ivanov Apr 01 '11 at 14:01
  • Every single time I run into this it turns out that I mistyped the package or class name. Can you cut and paste the actual package line from the .java file, the class declaration, and the actual XML line where you reference the class? – slund Apr 01 '11 at 14:06

2 Answers2

11

You should write it like:

<view class="com.hitziger.barcode.MyView"...
MByD
  • 135,866
  • 28
  • 264
  • 277
  • How can I access this then within my code? myView = (MyView) findViewById(R.id.my_view); gives me a force close when I run the program on the emulator. – hitzi Apr 01 '11 at 15:17
  • look at the LogCat error message and post it here. you can solve your own problems most times when you see what the error log says.. – james Apr 01 '11 at 15:21
  • In short, in your activity: `setCotentView(R.layout.layout); MyView myView = (MyView)findViewById(R.layout.my_view);` Read here more about it: http://developer.android.com/guide/topics/ui/declaring-layout.html. – MByD Apr 01 '11 at 15:22
0

in the layout.xml, use:

<View 
    android:class="com.hitziger.barcode.MyView"
    android:id="@+id/my_view"
...

istead of:

<com.hitziger.barcode.MyView
    android:id="@+id/my_view"
foolo
  • 804
  • 12
  • 22