Click here to Skip to main content
12,239,973 members (71,790 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java Mobile Android
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:
@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:
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 22-Dec-12 11:42am
halabella1.3K
Edited 22-Dec-12 23:47pm
v6

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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
  Permalink  
v2
Comments
halabella 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.
halabella 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.
halabella 23-Dec-12 9:30am
   
onStart() - doesn't matter, I tried to execute commands manually by pressing button, and it not work properly.
halabella 24-Dec-12 8:20am
   
Nobody knows?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100