Click here to Skip to main content
15,897,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    Button btn;
    SurfaceView sv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.button1);
        sv =  (SurfaceView)findViewById(R.id.surfaceView1);
        sv.setOnTouchListener( new SurfaceView.OnTouchListener(){
            private boolean moving = false;//stupid state
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch( event.getAction() ){
                    case MotionEvent.ACTION_DOWN:
                        final int x = (int)event.getX();
                        final int y = (int)event.getY();
                        final SurfaceHolder mHolder;
                        mHolder = sv.getHolder();
                        Canvas canvas = null;
                        canvas = mHolder.lockCanvas();
                        Paint mPaint = new Paint();
                        mPaint.setColor(Color.WHITE);
                        try{
                        canvas.drawText("some text", 50, 50, mPaint);
                        }catch (Exception xx)
                        {
                            xx.toString();//canvas = null
                        }
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        if( moving ){
                            final int x_new = (int)event.getX();
                            final int y_new = (int)event.getY();
                                }

                        return true;
                    case MotionEvent.ACTION_UP:
                        moving = false;
                        return true;
                }
                return false;
            }
        });
    }

I just want to draw some info when I touched or moved. I don't want to use OnDraw method because it eat CPU.
Edited as was suggested:
Java
@Override
protected void onPause()
{
    mHolder = sv.getHolder();
    canvas = null;
    canvas = mHolder.lockCanvas();
}

But it not helped, this func doesn't executes, canvas still = null.
I also tried this, in button OnClick:
Java
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mHolder = sv.getHolder();
canvas = null;
canvas = mHolder.lockCanvas();
try{
   canvas.drawText("some text", 100, 100, mPaint);
}catch (Exception xx)
{
  xx.toString();
}

- When I first click on button canvas != null, but after canvas.drawText execution go in nowhere, when I make second click - canvas = null. My brain corrode.
Posted
Updated 22-Dec-12 22:47pm
v6

1 solution

Java
SurfaceView.getHolder()


can not hold a valid value while you are still in the onCreate() method. It's not yet initialized at that runtime.

Activity has other methods

Java
protected void onResume(); // before Activity run
protected void onPause(); // after Activity run


That's a better place to interact with the SurfaceView.

Please also read in the documentation about these lifecycle controling methods:

Activity[^] @ Android Documentation
 
Share this answer
 
v2
Comments
[no name] 23-Dec-12 4:49am    
Thanks. Please read my question once again, I have edited my code as you suggested, but without result.
TorstenH. 23-Dec-12 5:04am    
Please read the documentation about Activity. It is well explained when which function is called.
[no name] 23-Dec-12 5:58am    
Have read, tried, it's helpless. I don't want to do like this http://stackoverflow.com/questions/5650810/canvas-and-surfaceview-example-crash-freeze-memory-leak first because it eat CPU, second I will unable to add some Buttons or other lements, and unable to switch to another layout because then TouchEvents will not working.
TorstenH. 23-Dec-12 6:53am    
so you also tried to use the onStart() method?

Really. I also have books like that on the shelf - but some things need to be read.
[no name] 23-Dec-12 9:30am    
onStart() - doesn't matter, I tried to execute commands manually by pressing button, and it not work properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900