I have an Android SurfaceView and in this I am trying to add buttons to this. In canvasView Surface I draw something. And I have a stream class to keep drawing.
package com.androidsurfaceview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class androidsurfaceview extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button buttonShowHide = (Button)findViewById(R.id.showhide); final Button buttonDummy = (Button)findViewById(R.id.dummy); buttonShowHide.setOnClickListener( new Button.OnClickListener(){ @Override public void onClick(View arg0) {
Stream class
package com.androidsurfaceview; import android.graphics.Canvas; import android.view.SurfaceHolder; public class MySurfaceThread extends Thread { private SurfaceHolder myThreadSurfaceHolder; private com.androidsurfaceview.test.MySurfaceView myThreadSurfaceView; private boolean myThreadRun = false; public MySurfaceThread(SurfaceHolder surfaceHolder, com.androidsurfaceview.test.MySurfaceView surfaceView) { myThreadSurfaceHolder = surfaceHolder; myThreadSurfaceView = surfaceView; } public void setRunning(boolean b) { myThreadRun = b; } @Override public void run() {
SurfaceView & drawing class
package com.androidsurfaceview; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; public class test extends Activity{ // ...... // I do a few things here... with this class test. public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{ private MySurfaceThread thread; private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); int cx, cy, offx, offy; public MySurfaceView(Context context) { super(context); // TODO Auto-generated constructor stub init(); } public MySurfaceView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub init(); } public MySurfaceView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub init(); } private void init(){ getHolder().addCallback(this); thread = new MySurfaceThread(getHolder(), this); setFocusable(true); // make sure we get key events paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(3); paint.setColor(Color.WHITE); cx = 0; cy = 0; offx = 10; offy = 10; } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub thread.setRunning(true); thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub boolean retry = true; thread.setRunning(false); while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { } } } ////Just a simple graphic of moving circle. @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub canvas.drawRGB(0, 0, 0); canvas.drawCircle(cx, cy, 3, paint); cx += offx; if (cx > getWidth() || (cx < 0)){ offx *= -1; cx += offx; } cy += offy; if (cy > getHeight() || (cy < 0)){ offy *= -1; cy += offy; } } } }
Here is main.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"> <Button android:id="@+id/showhide" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Toggle The Another Button Show/Hide" /> <Button android:id="@+id/dummy" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="a Button" /> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.androidsurfaceview.test.MySurfaceView android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> </LinearLayout>
PROBLEM: the above code works ONLY if I don't have "MySurfaceView" as a nested class. but with the external class "test" I get the following error. If I remove the "class test", it will work fine.
Error / Crash
04-29 11:43:18.977: ERROR/AndroidRuntime(21832): FATAL EXCEPTION: main 04-29 11:43:18.977: ERROR/AndroidRuntime(21832): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidsurfaceview/com.androidsurfaceview.androidsurfaceview}: **android.view.InflateException: Binary XML file line
Any help would be great ... I am stuck with this.