Android findViewById在自定义视图中返回null

Android findViewById在自定义视图中返回null,android,Android,好的,我已经了解了自定义视图必须有一个带有AttributeSet的构造函数才能使findViewById()工作。我还没有看到任何实际的实现 以下是我的代码: 扩展水平滚动视图的自定义视图: public AnimatedMenuBar(Context context) { // TODO Auto-generated constructor stub this(context, null); } public AnimatedMenuBar(

好的,我已经了解了自定义视图必须有一个带有AttributeSet的构造函数才能使findViewById()工作。我还没有看到任何实际的实现

以下是我的代码: 扩展水平滚动视图的自定义视图:

public AnimatedMenuBar(Context context) {
        // TODO Auto-generated constructor stub
        this(context, null);
    }

    public AnimatedMenuBar(Context context, AttributeSet attrs) {
        // TODO Auto-generated constructor stub
        this(context, attrs, 0);
    }

    public AnimatedMenuBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main, this);
        this.setHorizontalScrollBarEnabled(false);
    }
从我的主要活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testing);
        AnimatedMenuBar animatedMenuBar = (AnimatedMenuBar)findViewById(R.id.animatedMenuBar);
}
testing.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="match_parent">
    <com.pt.task.custommenubar.AnimatedMenuBar
        android:id="@+id/animatedMenuBar" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content" >
    <LinearLayout android:id="@+id/horizontalLayout" android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/dummyTextView" android:padding="5dip"/>
    </LinearLayout>
</merge>

animatedMenuBar总是空的,有什么我遗漏的吗


多谢各位

我遇到过这样的错误

  • 尝试项目清理。有时它会有帮助
  • 如果在custommenubar中嵌入了AnimatedMenuBar,则编写com.pt.task.custommenubar$AnimatedMenuBar
  • 尝试在代码中的任何地方重命名AnimatedMenuBar

  • 我遇到过这样的错误

  • 尝试项目清理。有时它会有帮助
  • 如果在custommenubar中嵌入了AnimatedMenuBar,则编写com.pt.task.custommenubar$AnimatedMenuBar
  • 尝试在代码中的任何地方重命名AnimatedMenuBar

  • 顺便说一句,这可能不适合问 这里,你能给我解释一下 视图可以像我一样简单地绘制 在 构造函数,不返回 什么都行阿尔文

    当您调用此
    充气器.inflate(R.layout.main,this)
    时,充气器将充气
    main.xml
    ,并将充气的
    视图
    放入自定义视图中的
    视图组

    它与活动中的
    setContentView(R.layout.main)
    具有相同的效果


    顺便说一句,这可能不适合问 这里,你能给我解释一下 视图可以像我一样简单地绘制 在 构造函数,不返回 什么都行阿尔文

    当您调用此
    充气器.inflate(R.layout.main,this)
    时,充气器将充气
    main.xml
    ,并将充气的
    视图
    放入自定义视图中的
    视图组


    它与活动中的
    setContentView(R.layout.main)
    具有相同的效果

    您可以发布XML布局:R.layout.testing吗?在问题详细信息中添加布局文件。您可以发布XML布局:R.layout.testing吗?在问题详细信息中添加布局文件。天哪,清理项目完成了任务。谢谢顺便说一句,这里可能不适合问这个问题,你能解释一下视图是如何绘制的,因为我只是在构造函数中调用了inflate(),而没有返回任何内容。天哪,清理项目完成了任务。谢谢顺便说一句,这里可能不适合问这个问题,你能解释一下视图是如何绘制的,因为我只是在构造函数中调用了inflate(),而没有返回任何内容。