布局未在android自定义组件中夸大 [英] Layout not being inflated in android custom component

查看:87
本文介绍了布局未在android自定义组件中夸大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义视图(它是从LinearLayout派生的)中遇到了空指针异常,因为它找不到其子视图.这是代码:

I'm getting a null pointer exception in my custom view (which is derived from a LinearLayout) because it can't find its child views. Here is the code:

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文件的方式:

And here is how I put it in the XML file:

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

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

But when I try to preview it in the layout editor I get an NPE on mText.setText(...) because getViewById() returns null.

这是怎么回事?

我希望这行得通的原因是,如果我这样做

The reason I expect this to work, is if I do

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

一切正常.那不是布局充气器在通过布局文件时的功能吗?无论如何,如何正确处理这两种情况(而不会获得毫无意义的嵌套视图)?

everything works fine. Is that not what the layout inflater does when it goes through a layout file? In any case, how can I handle both situations correctly (without getting pointless nested views)?

推荐答案

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

In you XML file you are trying to use a custom view class (com.example.views.MyView) and in the same time trying to add a TextView inside. It's not possible.

这是您需要更改的内容:

Here is what you need to change:

您必须在代码中添加XML文件:

You must inflate XML file in the code:

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

并按如下所示修改XML布局文件:

And modify the XML layout file like this:

<?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>

这篇关于布局未在android自定义组件中夸大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆