添加自定义ImageView后强制关闭

时间:2010-11-25 15:31:36

标签: android xml imageview android-imageview android-custom-view

我正在尝试向我的ImageView添加自定义main.xml,但是如果我启动该程序,它会以强制关闭方式关闭。

XML

<?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"
     android:background="@drawable/background" >

     <test.testpkg.CustomImageView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_gravity="center_vertical"
          android:src="@drawable/bg"/>

</LinearLayout>

Java

package test.testpkg;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;

public class CustomImageView extends ImageView {

    public CustomImageView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

另外如果我在FC之后在调试器中启动程序我只得到这个: link text

1 个答案:

答案 0 :(得分:3)

如果您没有附加Android的源代码,调试器就没用了。而且......提供logcat输出更有用。无论如何,我认为你缺少一个构造函数。试试这个:

public class CustomImageView extends ImageView {

 public CustomImageView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

 public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
 }
 // rest of your code....