我在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>

在家庭反应性中,我在on Create方法中有这样的电话。

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

当调用SetContentView方法时,APP力关闭。看来我的主XML不正确。

有任何想法吗?

有帮助吗?

解决方案

你是说它没有到达

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

其他提示

检查您的主页实施的构造函数:

 public HomeView(Context context, AttributeSet atts) {
     super(context, atts); 
 } 
<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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top