android自定义组件中的布局未膨胀

android自定义组件中的布局未膨胀,android,custom-component,android-custom-view,layout-inflater,Android,Custom Component,Android Custom View,Layout Inflater,我的自定义视图(从LinearLayout派生)中出现空指针异常,因为它找不到子视图。代码如下: public class MyView extends LinearLayout { public MyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyView(Context context, AttributeSet attrs, in

我的自定义视图(从
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.");
        }
    }
}
public MyView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.<your_layout>.xml, this);
}
以下是布局(
my_view.xml
):


一切正常。布局充气器在浏览布局文件时不是这样做的吗?在任何情况下,如何正确处理这两种情况(而不获取无意义的嵌套视图)?

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

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

您必须在代码中膨胀XML文件:

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.");
        }
    }
}
public MyView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.<your_layout>.xml, this);
}
publicMyView(上下文、属性集属性、int-defStyle)
{
超级(上下文、属性、定义样式);
LayoutInflater.from(context).inflate(R.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>


哪里是“”,为什么您的“android:id”语句没有在布局中首先声明它?但是如果我这样做了
myview=(myview)inflater.inflate(R.layout.my_view,null,false)
在其他一些代码中工作正常。也许这是布局编辑器中的一个bug(不是第一个!)。另外,如果你想按照你写的方式来做,你应该使用
,当然它是有效的。只是在
XML元素中放入的任何内容都将被忽略。这就是找不到TextView的原因。不,我的意思是,如果我随后执行
myview.findViewById(R.id.text)
,则可以找到该视图。如果布局充气器直接对自定义组件进行充气,则其行为似乎与在另一个布局中对自定义组件进行充气时有所不同。我将更新问题以澄清。您确定您应该拥有
.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>