1

我有一个应用程序,它的活动有一个扩展视图的内部类。视图类显示一个位图,一切正常。我现在要做的是在显示位图的视图上放置一个按钮。为此,我将原始内部类 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>
4

1 回答 1

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>
于 2011-05-09T11:56:41.027 回答