When my button is pressed, I get the error ViewRootImpl$CalledFromWrongThreadException - java

I tried to make a simple game on Android. The player is a blue ball and is supposed to avoid the moving objects. Once it hits it, the game is over. The game also ends if the player catches all the food objects. I got the collision detection working but when I tried to add a button afterwards to give the user the option to restart, I got the ViewRootImpl$CalledFromWrongThreadException error. The button is already declared in the onCreate method and the visibility is set to invisible, but when I change it to visible, I get the error. I have no idea how to fix it. I tried looking online and it wasn't much help. I understood that the error occcurs when you modify the UI in a non-UI thread, but how would you fix that? Any help would be appreciated. (:
Here is the run method:
#Override
public void run() {
// TODO Auto-generated method stub
while(locker==true){
//checks if the lockCanvas() method will be success,and if not, will check this statement again
if(!holder.getSurface().isValid()){
continue;
}
/** Start editing pixels in this surface.*/
Canvas canvas = holder.lockCanvas();
draw(canvas);
if (up==true){
y=y-3;
}
else if(down==true){
y=y+3;
}
else if (left==true){
x=x-3;
}
else if (right==true){
x=x+3;
}
int counter=0;
for(int i=0;i<listOfFs.length;i++)
{
foodCaught [i] = listOfFs [i].check (x,y,radiusBlack);
if (foodCaught [i] == true)
{
alive [i] = listOfFs [i].kill ();
}
else{
counter++;
}
}
if (counter==0){
win=true;
playAgain.setVisibility(View.VISIBLE);
}
for(int i=0;i<listOfCs.length;i++)
{
if (i<3){
listOfCs[i].changeX(listOfCs[i].getX()+increment[i]);
}
else{
listOfCs[i].changeY(listOfCs[i].getY()+increment[i]);
}
}
for(int i=0;i<listOfCs.length;i++){
if (y+radiusBlack>listOfCs[i].getY()&& y-radiusBlack<listOfCs[i].getY()+listOfCs[i].getSize()&& x-radiusBlack<listOfCs[i].getX()+listOfCs[i].getSize()&& x+radiusBlack>listOfCs[i].getX())
{
chaserCaught = true;
playAgain.setVisibility(View.VISIBLE);
}
}
// End of painting to canvas. system will paint with this canvas,to the surface.
holder.unlockCanvasAndPost(canvas);
}
}

You can't touch views outside UI thread (and your SurfaceView drawning happens on non-UI thread).
Just instead of doing this:
playAgain.setVisibility(View.VISIBLE);
do this:
playAgain.post(new Runnable() {
#Override
public void run() {
playAgain.setVisibility(View.VISIBLE);
}
});

Related

Audio not playing within thread Android

So i've got an audio file that i'd like to play within a thread, this is where i've left the comment // Tried playing audio here . But the even with the logic to make sure that it only plays once no audio is being played. Instead it seems like it's forcing the application to skip frames instead causing the game to have a few glitches because of the inconsistent frame rate.
Here's the code which i've been using.
while (isRunning) {
// Go back to the if statement and check if the surface still isn't valid
if (!mySurfaceHolder.getSurface().isValid())
continue;
// Lock the canvas for editing, make changes and unlock with new changes
final Canvas canvas = mySurfaceHolder.lockCanvas();
canvas.drawRect(0,0,canvas.getWidth(), canvas.getHeight(), pWhite);
// Create the block to check the answer
answerObject = new AnswerObject(canvas);
animalObject.move(canvas, tilt);
if(answerObject.onCollision(animalObject) != null){
// Inside of box else is outside of box
if (answerObject.onCollision(animalObject)) {
// If the animal is part of that enviroment
if (verifyEnviroment(enviromentObject.getAnimals())){
isCorrect = true;
} else {
isCorrect = false;
}
} else {
// If the animal is part of that enviroment
if (!verifyEnviroment(enviromentObject.getAnimals())){
// Tried playing audio here
isCorrect = true;
} else {
// Tried playing audio here
isCorrect = false;
}
}
gameActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (isCorrect){
scoreObject.setScore(10);
} else {
healthObject.setHealth(10);
healthObject.setHealthImgs(healthObject.getHealth());
if (healthObject.getHealth() <= 0){
isRunning = false;
}
}
resetGameObjects();
}
});
}
mySurfaceHolder.unlockCanvasAndPost(canvas);
}

Count integer with Thread in Activity and update Canvas in View

I'm developing my first android game without xml files for layouts in eclipse. This is calling MyGameView.class(extends View) from MyGameActivity.class(extends View implements OnTouchListener)
MyGameView myView;
myView = new MyGameView(this);
myView.setOnTouchListener(this);
setContentView(myView);
Everytime, I touch (MotionEvent.ACTION_DOWN), I update the Canvas in MyGameView.class with the following code. This is the code I use to update MyGameView.class from MyGameActivity.class.
myView.invalidate();
Yes it works!But I got a serious problem now.I need to move Canvas in MyGameView.class by every 0.3 second.I use Thread to update but it forced stop!!!! I tried again and again and again! I failed!
public void runtime() {
i = 0;
Thread timer = new Thread(new Runnable() {
#Override
public void run() {
while (i<1500) {
try {
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
if (right) {
i++;
//This is static int a from MyGameView.
MyGameView.a += 2;
myView.invalidate();
}
else {
i = 1500;
}
}
}
}
});
timer.start();
}
Updating Static int or boolean to MyGameView.class from MyGameActivity.class with myView.invaldate(); is working. But I can't control it with Thread! Is there something wrong with my Thread running code?
It's probably up to Thread because I can move +2 along X-axis to right everytime I press (MotionEvent.ACTION_DOWN).
//I can Move with this code but not with Thread!
case MotionEvent.ACTION_DOWN:
MyGameView.a += 2;
myView.invalidate();
break;
With Thread, it stops! Please give me a solution for this! In other words, I need to count with time without error and update canvas on every count!
Invalidate must be called from the UI thread. Try calling
postInvalidate()
instead.
See this for reference.

How to update values while method is running - Java

So I have this really weird problem that I simply cannot understand why it doesn't work. I am building a strobe light as part of my app and have created a separate Strobelight class. When I call the turnOn method or update method, the intervals never are changed. I guess it would make it easier to explain using some code:
public class Strobelight{
private int delayOn, delayOff;
public void turnStrobeOn(){...}
public void update(int a_delayOn, int a_delayOff){
delayOn = a_delayOn;
delayOff = a_delayOff;
}
public void turnOn(int a_delayOn, int a_delayOff){
delayOn = a_delayOn;
delayOff = a_delayOff;
this.turnStrobeOn();
}
Depending on whether the strobe light is already on or not, one of these methods are called to change turn on the strobe light with the specified intervals or simply to change the intervals.
Instead of changing the intervals to something custom, the application just uses the smallest possible intervals when calling Thread.sleep() for the flashlight being on or off
EDIT: this is the thread code, and code that turns the flashlight on
public void turnStrobeOn(){
for ( int i = 0; i < 3; i++){
isInCycle = true;
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview(); // the flashlight is now on
lightIsOn = true;
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(delayOn);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
cam.stopPreview();
cam.release();
lightIsOn = false;
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(delayOff);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
} //end of for loop
isInCycle = false;
} // end of turnStrobeOn
You might want to get someone else's advice on this buy my best guess is that you can't be so specific on sleep(). Also don't forget to use miliseconds for that. I'd suggest using the android handler and doing postDelayed for this instead of sleeping.
How to pause / sleep thread or process in Android?

Make a seek bar for media player.

I am making a media player using JMF, I want to use my own control components
Can anyone please help me in making a seek bar for media player so that it can play song according to the slider position.
Just suggest me some logic, I can figure out the coding part afterwards
if(player!=null){
long durationNanoseconds =
(player.getDuration().getNanoseconds());
durationbar.setMaximum((int) player.getDuration().getSeconds());
int duration=(int) player.getDuration().getSeconds();
int percent = durationbar.getValue();
long t = (durationNanoseconds / duration) * percent;
Time newTime = new Time(t);
player.stop();
player.setMediaTime(newTime);
player.start();
mousedrag=true;
Here is the code. Now how can I make the slider move along with the song?
Slider works when I drag/click on it, but it doesn't move with the song.
The problem with using a slider for this is that when the slider position is moved programmatically, it fires events. When an event is fired on a slider, it typically means the app. has to do something, such as move the song position. The effect is a never ending loop. There is probably a way around this by setting flags and ignoring some events, but I decided to go a different way.
Instead I used a JProgressBar to indicate the location in the track, and a MouseListener to detect when the user clicks on a separate position. Update the progress bar use a Swing Timer that checks the track location every 50-200 milliseconds. When a MouseEvent is detected, reposition the track.
The bar can be seen in the upper right of this GUI. Hovering over it will produce a tool tip showing the time in the track at that mouse position.
You could use a JSlider.
You can learn more from the Slider tutorial
You don't have to revalidate the container in order to change the slider.
Use these lines each time a new player is created:
slider.setMinimum(0);
slider.setMaximum(duration);
slider.setValue(0);
new UpdateWorker(duration).execute();
where duration is the variable holding the duration of the song in seconds.
And here is the code (used as inner class) which updates the slider:
private class UpdateWorker extends SwingWorker<Void, Integer> {
private int duration;
public UpdateWorker(int duration) {
this.duration = duration;
}
#Override
protected Void doInBackground() throws Exception {
for (int i = 1; i <= duration; i++) {
Thread.sleep(1000);
publish(i);
}
return null;
}
#Override
protected void process(List<Integer> chunks) {
slider.setValue(chunks.get(0));
}
}
Now the slider will move to the right until the end of the song.
Also note that unless you want to use a custom slider, JMF provides a simple (and working) slider via player.getVisualComponent() (see this example).
UPDATE
In order to pause/resume the worker thread (and thus the slider and the song), here is an example with a button that sets the appropriate flags.
private boolean isPaused = false;
JButton pause = new JButton("Pause");
pause.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
if (!isPaused) {
isPaused = true;
source.setText("Resume");
} else {
isPaused = false;
source.setText("Pause");
}
}
});
The method doInBackground should be changed to something like that:
#Override
protected Void doInBackground() throws Exception {
for (int i = 0; i <= duration; i++) {
if (!isPaused) {
publish(i);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
while (isPaused) {
try {
Thread.sleep(50);
continue;
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
Modify it accordingly to pause/resume the song along with the slider.
You should also consider #AndrewThompson's answer.

Rectangle colouring logic

I have one three rectangles in my canvas. I wanted to change the colours of three rectangles
in a slow manner one by one.
For example: When starting the application, user should be able to see three rectangles with the same colour (blue).
After 2 secons that rectangles colour should change to red.
Again after 2 secons the next rectangles colour should get changed.
The last one is also done the same way, that means after 2 seconds of the 2nd rectangle.
I wrote in my own way. But it is not working. All the rectanlges are changed together. I want one by one.
Could anyone give me the logic.
final Runnable timer = new Runnable() {
public void run() {
//list of rectangles size =3; each contain Rectangle.
for(int i = 0 ; i < rectangleList.size();i++){
if(rectangleListt.get(i).getBackgroundColor().equals(ColorConstants.blue)){
try {
rectangleList.get(i).setBackgroundColor(ColorConstants.yellow);
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//rectSubFigureList.get(i).setBorder(null);
}/*else{
rectSubFigureList.get(i).setBackgroundColor(ColorConstants.blue);
}*/
}
You're likely calling Thread.sleep inside of Swing's event thread or EDT (for event dispatch thread), and this will cause the thread itself to sleep. Since this thread is responsible for all of Swing's graphics and user interactions, this will in effect put your entire application to sleep, and is not what you want to have happen. Instead, read up on and use a Swing Timer for this.
References:
Swing Timer tutorial
Swing Event Dispatch Thread and Swingworker tutorial
To expand on Hidde's code, you could do:
// the timer:
Timer t = new Timer(2000, new ActionListener() {
private int changed = 0; // better to keep this private and in the class
#Override
public void actionPerformed(ActionEvent e) {
if (changed < rectangleList.size()) {
rectangleList.setBackgroundColor(someColor);
} else {
((Timer) e.getSource()).stop();
}
changed++;
}
});
t.start();
You can set a Timer:
// declaration:
static int changed = 0;
// the timer:
Timer t = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Change the colour here:
if (changed == 0) {
// change the first one
} else if (changed == 1) {
// change the second one
} else if (changed == 2) {
// change the last one
} else {
((Timer) e.getSource()).stop();
}
changed ++;
}
});
t.start();

Categories