10

I've defined a custom ViewGroup that extends the functionality of a LinearLayout:

public class TestLayout extends LinearLayout {

    public TestLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater inflater = (LayoutInflater)context.getSystemService
                  (Context.LAYOUT_INFLATER_SERVICE);

        inflater.inflate(R.layout.testlayout, this, true);
    }

}

The layout it inflates (testlayout.xml) looks like this:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>

</LinearLayout>

And finally i'm using this custom component in my main.xml:

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

    <my.test.namespace.TestLayout
        android:id="@+id/testLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </my.test.namespace.TestLayout>

</LinearLayout>

When viewing my main.xml in the layout editor, eclipse throws an error: my.test.namespace.TestLayout failed to instantiate.

And the stacktrace:

android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F030001.
    at com.android.layoutlib.bridge.android.BridgeResources.throwException(BridgeResources.java:648)
    at com.android.layoutlib.bridge.android.BridgeResources.getLayout(BridgeResources.java:270)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
    at my.test.namespace.TestLayout.<init>(TestLayout.java:18)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:397)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:165)
    at com.android.layoutlib.bridge.android.BridgeInflater.loadCustomView(BridgeInflater.java:205)
    at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:133)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)

0x7F030001 points to the layout xml file in R.java. I tried cleaning my project, but that did nothing. Am I using the LayoutInflater wrong, or what is going on here?

soren.qvist
  • 7,376
  • 14
  • 62
  • 91
  • see show view > error log to see the error on xml file – Shruti Dec 10 '11 at 11:57
  • @DroidShruti "my.test.namespace.TestLayout failed to instantiate." is what it says in the error log. – soren.qvist Dec 10 '11 at 16:41
  • 1
    Your layout works perfectly for me. Try to both clean your project (again) and restart eclipse, as well as refresh your folders (from eclipse)... – Jave Dec 10 '11 at 22:34
  • @Jave That seemed to work, thank you! You should create an answer if you want the 50 rep. awarded (I can't award them to comments). – soren.qvist Dec 11 '11 at 01:58
  • I just did. (for some reason a lot of my 'have you tried to...'-comments become the correct answers :p ) – Jave Dec 11 '11 at 11:31

1 Answers1

24

The following usually help:

  • Clean your project (again, just in case).
  • Restart eclipse.
  • Refresh your folders in eclipse (right click -> refresh, or select your top folder and press f5).
Jave
  • 31,598
  • 14
  • 77
  • 90
  • 4
    Only restarting of Eclipse fixed the problem for me. None of there others! – WindRider Feb 12 '13 at 20:32
  • If you are trying to use resources in your custom view initialization code and want to still render in the graphical layout preview then these steps wont work - see http://stackoverflow.com/questions/11543855/resourcesnotfoundexception-in-graphical-layout-adt-preview-but-app-actually-wo/17742353 instead – Cel Jul 19 '13 at 09:23
  • Eclipse and Android resource management feels more and more like fixing Windows problems... restart seems often to be the solution. – Knickedi Jun 13 '14 at 07:57
  • As comments before say. Off and back on again, like Windows. Getting hang of this, didn't even have to wait 10secs before restarting. – John Sep 17 '14 at 11:18