Reputation: 7574
I've an app where it's activity has an inner class that extends view. the view class displays a bitmap and it all works fine. What i'm trying to do now is place a button on the view that diaplays tha bitmap. to do this i made the original inner class TouchView a seperate class. i then declared it in an xml file in the layout folder inside a relelative layout and beneath a button. this i thought would give there appearance of the button floating on top of the view. i'm getting the following error. does anyone know why? thanks
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)
Code:
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);
}
}
enter code here
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
}
Layout:
<?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>
Upvotes: 1
Views: 2771
Reputation: 15269
Change your both constructor to this
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
Change you xml to this
<?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>
Upvotes: 1