Android FindViewByID为CustomView返回null

Android FindViewByID为CustomView返回null,android,null,Android,Null,我在android中再次遇到findViewById问题。 我的活动从不退出while循环,因为listview总是空的。我已经试过重建了。我知道视图的onfinishedFlating方法,但它对我的活动没有帮助 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.list_with_compass);

我在android中再次遇到findViewById问题。 我的活动从不退出while循环,因为listview总是空的。我已经试过重建了。我知道视图的onfinishedFlating方法,但它对我的活动没有帮助

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.list_with_compass);

    ListWithCompassView lv = (ListWithCompassView) this.findViewById(R.id.compass_list);

    while(lv == null) {
        Log.d("waiting", "waiting1");
        lv = (ListWithCompassView) this.findViewById(R.id.compass_list);
    }
    this.setListView();
}
这是XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/linear_layout_compass" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <karsunke.view.ListWithCompassView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/compass_list">
    </karsunke.view.ListWithCompassView>
</LinearLayout>


这是一个漫长的过程,但请尝试清理项目并重建它。有时,在重建项目之前,我在findviewbyd中无缘无故地收到null。如果这无助于发布自定义类,那么我们可以查看问题是否存在。

将此构造函数添加到自定义视图类中

public ListWithCompassView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

CompassView代码的列表在哪里…??没关系,它只是一个扩展视图类的自定义视图。您是否尝试过使用层次结构查看器?您应该检查自定义视图是否在层次结构中,以及是否为其分配了标识符。我最近和你有同样的问题。谢谢你。这就是解决办法。Android膨胀XML文件的方式仍然让我有点困惑;)我添加了构造函数,但出于某种原因,我只调用了super(上下文)。由于这个原因,这个物体没有ID。哇,我不确定我是否会找到这个。在我的自定义视图的构造函数中,我以前只调用了super(context),没有属性集。在我需要使用findViewById获取视图之前,一切都很好。@FranziskusKarsunke:你在这里的“评论”对我来说是正确的答案:)这解决了我的问题,但对为什么需要这样做有什么好的解释吗?