【问题标题】:Layout not being inflated in android custom component布局没有在android自定义组件中膨胀
【发布时间】:2012-10-18 20:17:58
【问题描述】:

我的自定义视图(派生自LinearLayout)中出现空指针异常,因为它找不到它的子视图。代码如下:

public class MyView extends LinearLayout
{
    public MyView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    private TextView mText;

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        mText = (TextView) findViewById(R.id.text);

        if (isInEditMode())
        {
            mText.setText("Some example text.");
        }
    }
}

这是布局(my_view.xml):

<?xml version="1.0" encoding="utf-8"?>
<com.example.views.MyView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:ellipsize="end"
        android:maxLines="4"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Some text" />

</com.example.views.MyView>

这是我将它放入 XML 文件的方式:

    <com.example.views.MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

但是当我尝试在布局编辑器中预览它时,我在mText.setText(...) 上得到一个 NPE,因为getViewById() 返回null

发生了什么事?

澄清

我希望这样做的原因是,如果我这样做了

MyView v = (MyView)inflater.inflate(R.layout.my_view);
((TextView)v.findViewById(R.id.text)).setText("Foo");

一切正常。这不是布局充气器在通过布局文件时所做的吗?无论如何,我怎样才能正确处理这两种情况(没有得到毫无意义的嵌套视图)?

【问题讨论】:

  • '' 在哪里以及为什么'android:id' 不是你在布局中第一次声明它的地方吗?

标签: android custom-component android-custom-view layout-inflater


【解决方案1】:

在您的 XML 文件中,您尝试使用自定义视图类 (com.example.views.MyView),同时尝试在其中添加 TextView。这是不可能的。

以下是您需要更改的内容:

您必须在代码中扩充 XML 文件:

public MyView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.<your_layout>.xml, this);
}

并像这样修改 XML 布局文件:

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

<TextView
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:ellipsize="end"
    android:maxLines="4"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:text="Some text" />

</LinearLayout>

【讨论】:

  • 但如果我在其他代码中使用myview = (MyView)inflater.inflate(R.layout.my_view, null, false),它就可以正常工作。也许这是布局编辑器中的一个错误(不会是第一个!)。另外,如果您打算按照您所写的方式进行操作,则应该使用&lt;merge&gt;
  • 当然可以。只是您在&lt;com.example.views.MyView&gt; XML 元素中放入的任何内容都将被忽略。这就是找不到 TextView 的原因。
  • 不,我的意思是如果我再做myview.findViewById(R.id.text) 会发现视图很好。如果布局充气器直接充气自定义组件到当它充气自定义组件inside另一个布局时,布局充气器的行为似乎有所不同。我会更新问题以澄清。
  • 你确定你应该拥有.xml吗?
猜你喜欢
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多