我正在尝试添加自定义 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之后在调试器中启动该程序,我只会得到:链接文字

有帮助吗?

解决方案

如果您还没有附上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....
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top