【问题标题】:Android layout previewer throws error when adding custom LinearLayoutAndroid布局预览器在添加自定义LinearLayout时抛出错误
【发布时间】:2012-01-15 17:49:33
【问题描述】:

我定义了一个自定义 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 错误,或者这里发生了什么?

【问题讨论】:

  • 查看显示视图 > 错误日志以查看 xml 文件上的错误
  • @DroidShruti "my.test.namespace.TestLayout 无法实例化。"是它在错误日志中所说的。
  • 您的布局非常适合我。尝试(再次)清理您的项目并重新启动 Eclipse,以及刷新您的文件夹(从 Eclipse)...
  • @Jave 这似乎有效,谢谢!如果您想要 50 个代表,您应该创建一个答案。已授予(我不能将它们授予 cmets)。
  • 我刚做了。 (出于某种原因,我的很多'你试过......'-cmets成为正确答案:p)

标签: java android android-linearlayout layout-inflater


【解决方案1】:

以下通常会有所帮助:

  • 清理您的项目(同样,以防万一)。
  • 重启 Eclipse。
  • 在 Eclipse 中刷新文件夹(右键单击 -> 刷新,或选择 您的顶级文件夹,然后按 f5)。

【讨论】:

  • 只有重启 Eclipse 才能解决我的问题。没有其他人!
  • 如果您尝试在自定义视图初始化代码中使用资源并且仍希望在图形布局预览中呈现,那么这些步骤将不起作用 - 请参阅stackoverflow.com/questions/11543855/…
  • Eclipse 和 Android 资源管理越来越像修复 Windows 问题...重启似乎通常是解决方案。
  • 正如 cmets 之前所说。关闭然后重新打开,如 Windows。掌握这一点,甚至不必等待 10 秒即可重新启动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-19
相关资源
最近更新 更多