I have an android application that refreshes the screen every 33 milliseconds, displaying a rectangle at a pair of coordinates. Here is the code for the custom view:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class OurView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Handler handler = new Handler();
private Bitmap testimg;
private int xCoord = 500;
private int yCoord = 500;
boolean running = false;
int xPos;
int yPos;
public OurView(Context context) {
super(context);
init();
}
public OurView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OurView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
holder = getHolder();
holder.addCallback(this);
testimg = BitmapFactory.decodeResource(getResources(),R.drawable.testimg);
}
void moveImage(int xChange, int yChange) {
this.xCoord = this.xCoord + xChange;
this.yCoord = this.yCoord + yChange;
}
void doDraw(Canvas canvas) {
xPos = this.xCoord + (testimg.getWidth()/2);
yPos = this.yCoord + (testimg.getHeight()/2);
canvas.drawARGB(255, 55, 255, 255);
canvas.drawBitmap(testimg, xPos, yPos, null);
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
running = true;
while(running){
handler.postDelayed(new Runnable() {
public void run() {
Canvas canvas = getHolder().lockCanvas();
if(canvas != null){
synchronized (getHolder()) {
doDraw(canvas);
}
holder.unlockCanvasAndPost(canvas);
}
}
}, 33);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
I used to have a separate thread that handled the drawing, but that caused problems with variables having different values between the threads. When I run this program, I get this error:
04-20 10:54:35.577 1925-1931/com.thatoneprogrammerkid.gameminimum I/art﹕ Thread[2,tid=1931,WaitingInMainSignalCatcherLoop,Thread*=0xae668400,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3
04-20 10:54:35.577 1925-1931/com.thatoneprogrammerkid.gameminimum I/art﹕ [ 04-20 10:54:35.627 1925: 1931 W/art ]
The easiest way to make sure you don’t miss out any error messages is to open Logcat Monitor’s Verbose dropdown and set it to Error, which will filter out everything except error messages. 1. R.layout.main Cannot Be Found / Cannot Resolve Symbol R
There’s a wide range of error messages you may encounter when testing your app, ranging from severe errors that will cause your app to crash the very first time you try to install it on a target device to more subtle errors that degrade your application’s performance over time.
Android shows ANR dialogs for apps that take too long to process the broadcast message only if Show all ANRs is enabled in the device’s Developer options. For this reason, background ANR dialogs are not always displayed to the user, but the app could still be experiencing performance issues.
Go to Settings and tap Apps & Notifications. Here, tap on See all apps and you will see the list of all the installed apps. Look for the Android System Web view and tap on it. Tap the three dots on the top right corner and then tap on Uninstall updates. On the pop-up box, tap OK. On most Android phones, this should fix the app crashing issue.
That's not an error, that's the VM letting you know that your app was sent a signal 3 (SIGQUIT). The most likely cause is that the app was unresponsive and the system is doing ANR processing -- SIGQUIT causes the VM to dump a stack trace.
Do you see ANR complaints in logcat?
Looking at your code, you're looping in surfaceCreated()
, which runs on the UI thread. That means your app won't be able to process messages from the system (or draw on Views, or receive user input). You can either use a separate thread as you did before (but with better synchronization), or just remove the loop from surfaceCreated()
and then have your draw handler re-issue postDelayed()
every time it executes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With