0

I am having problem with invoking a custom component in my Android projects in Eclipse. It seem that I do not understand how the namespace declarations belong together. I have checked several other threads here at SO, which at first seem to be related, but I cannot solve my problem with these:

I have the following set-up (code is anonymized):

/values/extra_attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="extraComponent">
        <attr name="count" format="integer" />
    </declare-styleable>
</resources>

/layout/extra_main.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">    
    <ImageView
        android:id="@+id/extra_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/extra_main" 
    />        
</merge>

/com.site.package.extra/extra.java

package com.site.package.extra;

... misc imports...

public class Extra extends FrameLayout
{
... misc code...
}

/com.site.package/main.java (start-up class)

package com.site.package;

... misc imports...

public class Main extends Activity
{
... misc code...
}

/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  style="@style/main_style" 
  xmlns:extra="http://schemas.android.com/apk/res/com.site.package" >

  <com.site.package.Extra
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    extra:count="3">
  </com.site.package.Extra>
</RelativeLayout>

The problem I face is; whatever I do, I cannot manage to invoke my custom component. The errors occur in may layout and I have tried to change the following items:

  1. name space declaration
    • xmlns:extra="http://schemas.android.com/apk/res/com.site.package"
    • xmlns:extra="http://schemas.android.com/apk/res/com.site.package.extra"
    • xmlns:extra="http://schemas.android.com/apk/res/extra"
  2. component invokation
    • <com.site.package.extraComponent />
    • <com.site.package.Extra.extraComponent />
    • <extraComponent />
    • <Extra.extraComponent />
    • <android.view.ext.extraComponent />
  3. attributes
    • extra:count="3"
    • com.site.package.extra:count="3"

In neither case I manage to get any help from intellisense so I am completely lost. I really do not understand how the namespaces are working here and how I should put the code to work.

EDIT :

I forgot to include my AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.site.package"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="7" />

  <application
    android:icon="@drawable/ic_logo"
    android:label="MyApp"
    android:name="MyApp" >

  <activity
    android:label="MyApp"
    android:name=".Main" >
    <intent-filter >
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
</manifest>
Community
  • 1
  • 1

1 Answers1

1

Your custom component is situated in the package com.site.package.extra(from the code you posted) so you could use it in the xml layout with:

<com.site.package.extra.Extra // ... other attributes

or with:

<view  class="com.site.package.extra.Extra"
     // ... other attributes />

The namespace for the custom attributes:

xmlns:extra="http://schemas.android.com/apk/res/com.site.package"

and to use them:

extra:count="3"
user
  • 85,380
  • 17
  • 189
  • 186
  • This seem to be a step forward. I think I actually tried this in one of my many tests, but since I cannot get intellisense to work I was assuming this was wrong and did not test run. Still intellisense does not provide any help, so I assume that there is still something worng. Well, I went on to try to run in debugger/emulator and then I got an exception: **Binary XML file line #13: Error inflating class com.site.package.extra.Extra**. Do you know why intellisense is not invoked and what situations can cause this error? The program does not give any errors or warnings in editor... :-( – Ray 'user1578904' Aug 10 '12 at 12:27
  • 1
    @Ray'user1578904' The intellisense will not work for your custom views, this is where you go manually. In the logcat below the inflating exception on the line 13 there should be a cause for that exception(like class not found, NullPointerException in constructor etc). See what it tells you. – user Aug 10 '12 at 12:30
  • I get this cause with the exception: **java.lang.ClassNotFoundException: com.site.package.extra in loader dalvik.system.PathClassLoader@44e8d200** – Ray 'user1578904' Aug 10 '12 at 12:44
  • I have learned a few things already. I was before confused about namespace belongings, and couldn't get into my head which namespace to use because I was assuming my custom attributes somehow needed to be linked to the component namespace. Also interesting to know that intellisense is not supposed to work here. – Ray 'user1578904' Aug 10 '12 at 12:46
  • 1
    @Ray'user1578904' In the exception you posted the package + class name is `com.site.package.extra`(which is how you probably have it in the xml layout), shouldn't that be `com.site.package.extra.Extra`? – user Aug 10 '12 at 12:54
  • you are right, at the same moment I posted the detailed error message I noticed the error. It seem to work now, and I will mark the issue as solved. – Ray 'user1578904' Aug 10 '12 at 12:59
  • May I add just one follow-up question? I can edit my layout xml in xml mode with no problem, but I was expecting to be able to render the result in the Graphical Layout. However, I get just a label with the class name and below I get an error "The following classes could not be instantiated: - com.site.package.extra.Extra (Open Class, Show Error Log) See the Error Log (Window > Show View) for more details. Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse". Is this normal? The code is working fine once compiled. – Ray 'user1578904' Aug 10 '12 at 12:59
  • 1
    @Ray'user1578904' Unfortunately I can't help you with that as I don't use the graphical editor that much. You could use that tip and implement the `isInEditMode` method for your `View` to return `true`. – user Aug 10 '12 at 13:05