Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
can you try to rebuild your project and deploy it? Seems like eclipse is messed up... –  Vladimir Ivanov Apr 1 '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 1 '11 at 14:06

2 Answers

up vote 8 down vote accepted

You should write it like:

<view class="com.hitziger.barcode.MyView"...
share|improve this answer
 
Thanks, this way works fine :) –  hitzi Apr 1 '11 at 15:03
 
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 1 '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.. –  binnyb Apr 1 '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: developer.android.com/guide/topics/ui/declaring-layout.html. –  Binyamin Sharet Apr 1 '11 at 15:22

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.