添加自定义 LinearLayout 时 Android 布局预览器抛出错误

发布于 2024-12-20 02:03:35 字数 3050 浏览 2 评论 0原文

我定义了一个自定义 ViewGroup 来扩展 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);
    }

}

它膨胀的布局 (testlayout.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" android:gravity="center">

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

</LinearLayout>

最后,我在我的 ma​​in 中使用这个自定义组件.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>

在布局编辑器中查看我的 main.xml 时,eclipse 抛出错误: my.test.namespace.TestLayout 实例化失败。

堆栈跟踪:

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 指向 R.java 中的布局 xml 文件。我尝试清理我的项目,但没有任何作用。我是否使用了 LayoutInflater 错误,或者这里发生了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2024-12-27 02:03:35

以下内容通常会有所帮助:

  • 清理您的项目(再次强调,以防万一)。
  • 重新启动日食。
  • 在 Eclipse 中刷新文件夹(右键单击 -> 刷新,或选择
    您的顶级文件夹并按 f5)。

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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文