Skip to content Skip to sidebar Skip to footer

PostInvalidate() Not Working With Non-ui Thread

I am using a custom view to draw a game content and i use a button from xml layout to enable or disable the drawing of a specific content(rectangle) using separate thread.I managed

Solution 1:

As explained in What is the difference between Android's invalidate() and postInvalidate() methods?, there might be some problems when postInvalidate is used from other threads.

I haven't tested this and it's not really nice, but it could help you (instead of this.postInvalidate();)

((MainActivity) gameContext).runOnUiThread(new Runnable() {
       @Override
       public void run() {

           Cview.this.invalidate();

       }
 });

Solution 2:

I just ran into the exact same issue.

The fix for me was to assign the custom view using findViewById() in onCreate(), instead of calling the constructor.

Add

android:id="@+id/custom_view"

to your XML, and

v = (Cview) findViewById(R.id.custom_view);

to your onCreate() function (and remove the constructor call).

Do this after calling setContentView().

Android has gazillions of silent failure modes like this.


Post a Comment for "PostInvalidate() Not Working With Non-ui Thread"