6 Replies - 1318 Views - Last Post: 14 February 2013 - 06:56 AM

#1 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

crash when using thread

Posted 12 February 2013 - 10:58 PM

Hi
I am starting to do some java programming for my android app development course, and I am starting with creating a pong game to start to learn the basics. I have written all this code my self only using the documentation I found online, so it may be sloppy, but I just started :)

Anyways, on to my issue. When I run this program, I instantly crash. at first I though it was because I wasn't specifying an fps, but that wasn't the case, because even when I got it to run only once every second it was still crashing. I also got it so my ball didn't move and all my game loop is doing is adding to a variable and it still crashed. Seeing as how eclipse is not very useful in telling me where my code is having troubles in, and I can't think of anything else, I ask you!
Here is the java code.
package com.example.pong;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	int player_X, player_Y, touch_Y, ball_X, ball_Y, ballSpeedX, ballSpeedY,fps;
	ImageView playerPaddle, computerPaddle, ball;
	RelativeLayout rlMain;
	TextView debug;
	View playingScreen;
	Boolean isRunning;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		callVariables();
		playerPaddle.setX(player_X);
		playerPaddle.setY(player_Y);
		runGame();
	}

	private void runGame() {
		// TODO Auto-generated method stub
		new Thread(new Runnable() {
			public void run() {
				while (isRunning) {
					onTouchEvent();
					//moveBall();
					try {
						Thread.sleep(fps);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ballSpeedY += 1;
					debug.setText(Integer.toString(ballSpeedY));
				}
			}
		}).start();

	}

	public void moveBall() {

		if (ball_X < 100) {
			ballSpeedX *= -1;
		} else if (ball_X > 1000) {
			ballSpeedX *= -1;
		}
		if (ball_Y < 50) {
			ballSpeedY *= -1;
		} else if (ball_Y > 600) {
			ballSpeedY *= -1;
		}
		ball_X += ballSpeedX;
		ball_Y += ballSpeedY;
		debug.setText(Integer.toString(ballSpeedY));
		//ball.setX(ball_X);
		//ball.setY(ball_Y);
	}

	private void onTouchEvent() {
		// TODO Auto-generated method stub
		playingScreen.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				touch_Y = (int) event.getY();

				movePaddle();
				return true;
			}
		});
	}

	private void movePaddle() {

		if ((player_Y + 75) > (touch_Y + 25)
				|| (player_Y + 75) < (touch_Y - 25)) {
			if ((player_Y + 75) < touch_Y) {
				player_Y += 10;
			} else if ((player_Y + 75) > touch_Y) {
				player_Y -= 10;
			}
		}

		playerPaddle.setX(player_X);
		playerPaddle.setY(player_Y);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private void callVariables() {
		// TODO Auto-generated method stub

		player_X = 50;
		player_Y = 350;
		ball_X = 600;
		ball_Y = 350;
		playerPaddle = (ImageView) findViewById(R.id.ivPlayerPaddle);
		rlMain = (RelativeLayout) findViewById(R.id.mainLayout);
		debug = (TextView) findViewById(R.id.debug);
		playingScreen = findViewById(R.id.mainLayout);
		ball = (ImageView) findViewById(R.id.ivBall);
		computerPaddle = (ImageView) findViewById(R.id.ivComputerPaddle);
		isRunning = true;
		ballSpeedX = 1;
		ballSpeedY = 1;
		fps = 1000;

	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: crash when using thread

#2 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: crash when using thread

Posted 13 February 2013 - 02:34 AM

Well, it's hard to tell from code ony what is responsible for the crash. I suggest you read some stuff up on debugging Android applications:

http://developer.and...ging/index.html
Was This Post Helpful? 0
  • +
  • -

#3 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: crash when using thread

Posted 13 February 2013 - 08:23 AM

View PostTheKirk, on 13 February 2013 - 02:34 AM, said:

Well, it's hard to tell from code ony what is responsible for the crash. I suggest you read some stuff up on debugging Android applications:

http://developer.and...ging/index.html

well all the code works. its just when i add the move ball function, or try to add numbers to a variable and diaplay it, in the gamr loop inside thr thread that i get a crash.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,088
  • Joined: 27-December 08

Re: crash when using thread

Posted 13 February 2013 - 01:18 PM

Moved to Android.

Post your stack trace from the LogCat.
Was This Post Helpful? 0
  • +
  • -

#5 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: crash when using thread

Posted 13 February 2013 - 02:11 PM

do you want me to paste the entire log cat? its kinda long... but here it is
also if this helps at the top of it it says:ViewRootImpl.checkThread() line:5003
dont know if this helps or not
Was This Post Helpful? 0
  • +
  • -

#6 mouthwash  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 12-February 13

Re: crash when using thread

Posted 13 February 2013 - 02:21 PM

also it says exception ViewRootImpl$CalledFromWrongThreadException
Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: crash when using thread

Posted 14 February 2013 - 06:56 AM

It looks like you are trying to change the UI from a thread.
Changes to the UI can only be done in the main thread (UI-thread)

The way to change the UI from a thread, is to run that specific line on the UI-thread:

MainActivity.this.runOnUiThread(new Runnable() {
        public void run() { 
         //Update your UI here (Like changing the text of a TextView or adding stuff)
        }
}); 


Cheers
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1