xml布局中的自定义视图无法正常工作

时间:2012-08-15 14:26:51

标签: android xml layout

我正在尝试将View的子类添加到我的activity_main.xml中,如this

但是,设备和模拟器会立即退出应用程序。错误似乎是错误膨胀类com.example.androidtest.PuzzleView

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

<com.example.androidtestapp.PuzzleView
    android:id="@+id/puzzleView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="60"/>

<include
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    layout="@layout/activity_input"/>

</LinearLayout>

package com.example.androidtestapp;


public class PuzzleView extends View {


public PuzzleView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);


}
}

1 个答案:

答案 0 :(得分:2)

我认为问题是您需要将带有AttributeSet attrs的构造函数作为参数

public PuzzleView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //code
}