0

我在 src > myproject.test > HomeView 中有一个自定义视图

在我的主要布局 xml 中,我有以下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <myproject.test.HomeView
        android:id="@+id/home_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    </myproject.test.HomeView>

</LinearLayout>

在 HomeActivity 中,我在 onCreate 方法中有这样的调用。

setContentView(R.layout.main);
HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

调用 setContentView 方法时应用强制关闭。看来我的主要 xml 不正确。

有任何想法吗?

4

3 回答 3

2

你的意思是它没有到​​达

HomeView mHomeView = (HomeView) this.findViewById(R.id.home_view);

并且撞车之前就行了吗?

检查您的构造函数是否

HomeView(final Context context, final AttributeSet attrs){
     super(context, attrs);

并不是

HomeView(final Context context){
     super(context);

你需要 AttributeSet

于 2010-10-21T22:24:36.703 回答
0

检查 HomeView 实现的构造函数:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 
于 2010-10-21T22:28:52.453 回答
0
<LinearLayout xmlns:android = 
      http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:orientation="vertical"

我的布局都没有@+id. 也许您应该将视图设置为 home_root. 请与您的R.java联系以获取布局的名称,或尝试

setContentView(R.layout.home_root);
于 2010-10-21T22:29:22.493 回答