在xml中声明时,自定义视图不起作用

时间:2011-05-09 11:18:30

标签: android android-layout custom-view

我有一个应用程序,它的活动有一个扩展视图的内部类。视图类显示一个位图,一切正常。我现在要做的是在视图上放置一个按钮来显示位图。要做到这一点,我使原来的内部类TouchView成为一个单独的类。然后我在布局文件夹中的xml文件中将其声明放在相关布局内和按钮下方。我认为这会让按钮的外观漂浮在视图的顶部。我收到以下错误。有谁知道为什么?感谢

05-09 11:45:22.603: ERROR/AndroidRuntime(12211): Uncaught handler: thread main exiting due to uncaught exception
05-09 11:45:22.613: ERROR/AndroidRuntime(12211): java.lang.NullPointerException
05-09 11:45:22.613: ERROR/AndroidRuntime(12211):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
05-09 11:45:22.613: ERROR/AndroidRuntime(12211):     at android.graphics.Canvas.drawBitmap(Canvas.java:980)
05-09 11:45:22.613: ERROR/AndroidRuntime(12211):     at com.tecmark.TouchView.onDraw(TouchView.java:172)
05-09 11:45:22.613: ERROR/AndroidRuntime(12211):     at android.view.View.draw(View.java:6535)

代码:

public class Jjilapp extends Activity {



    private static final String TAG = "*********jjil";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "***********inside oncreate about to set contentview = ");
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.touchview);


    }


}

在这里输入代码

class TouchView extends View{


    private File tempFile;
    private byte[] imageArray;
    private Bitmap bgr;
    private Bitmap bm;
    private Bitmap whitebm;
    private Paint pTouch;
    private int centreX = 1;
    private int centreY = 1;
    private int radius = 50;





    public TouchView(Context context, AttributeSet attr) {
        super(context,attr);
    }

    public TouchView(Context context) {
        super(context);

       .........code that gets bitmap and changes it 





}// end of touchView constructor


    public void changePixel(){  


        for (int i=0; i < bgr.getWidth(); ++i) {
            for (int y=0; y < bgr.getHeight(); ++y) {

    if( Math.sqrt( Math.pow(i - centreX, 2) + ( Math.pow(y - centreY, 2) ) ) <= radius ){

                    bgr.setPixel(i,y,Color.rgb(255,255,255));
                }
            }
        }   

        }// end of changePixel()


    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        ......code for touch events
    }//end of onTouchEvent


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

        canvas.drawBitmap(whitebm, 0, 0, null);
        canvas.drawBitmap(bgr, 0, 0, null);
        canvas.drawCircle(centreX, centreY, radius,pTouch);


    }//end of onDraw


}

布局:

<?xml version="1.0" encoding="utf-8" ?> 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical" android:layout_width="match_parent" 
                android:layout_height="wrap_content" android:gravity="fill">


   <Button android:text="Button" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </Button>

  <com.tecmark.TouchView
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 


  </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

将两个构造函数更改为此

public TouchView(Context context) {
    super(context);
    TouchView(context, null);
}

public TouchView(Context context, AttributeSet attr) {
    super(context,attr);

   .........code that gets bitmap and changes it 

}// end of touchView constructor

将xml更改为此

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="fill">

    <com.tecmark.TouchView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
   <Button
        android:text="Button"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    </Button>
</RelativeLayout>