Java Help

C++, C#, Java, PHP, ect...
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Java Help

Post by hallsofvallhalla »

I am trying to change the value of a TextView to the random number that is rolled. For some reason the program crashes when it hits the settext line. Is there something I am missing on outputting a variable to the settext? It does work when I use a string like "test"

Code: Select all

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
btn1=(Button)findViewById(R.id.Button01);
btn1.setOnClickListener(this);
tv01=(TextView)findViewById(R.id.tv01);

}
public void onClick(View view) {
rolld6();
}
public void rolld6() {
	Random randd6 = new Random();
	int d6roll = randd6.nextInt(6) + 1;
	tv01.setText(d6roll);  <-------messes up here
}

Code: Select all

tv01.setText("test");  <-------works
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Java Help

Post by hallsofvallhalla »

yipee figured it out

Code: Select all

 tv01.setText(""+d6roll); 
User avatar
kyraneth
Posts: 664
Joined: Thu Apr 23, 2009 2:56 pm

Re: Java Help

Post by kyraneth »

don't want to go OT, but could you point me to some easy beginner java tuts? Or any useful resource you have, i'm looking on learning java.
Just FYI, the post is above, so stop looking down here and do something useful.

king noob. Nice.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Java Help

Post by hallsofvallhalla »

hehe i am actually just googling the problems as they come. I have always found it easier just figure it out as I go. I started with a blank page and said, "well how do I create a button?", so google creating a button in java. After i got the button then I asked myself "How do I amke it clickable?" I then googled that, and so on and so....I have always learned much faster this way.


K new problem. How in the hell do you delay a animation???? Like I am drawing an image and I made a for loop to cycle through the images but at a slow enough speed to see. I have tried sleep() but it wont display the images, only the last one. I have tried timer but its all crazy...ughghghg
User avatar
kyraneth
Posts: 664
Joined: Thu Apr 23, 2009 2:56 pm

Re: Java Help

Post by kyraneth »

that's a good method, i should try it out, thanks :D
Just FYI, the post is above, so stop looking down here and do something useful.

king noob. Nice.
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Java Help

Post by Perry »

hallsofvallhalla wrote: K new problem. How in the hell do you delay a animation???? Like I am drawing an image and I made a for loop to cycle through the images but at a slow enough speed to see. I have tried sleep() but it wont display the images, only the last one. I have tried timer but its all crazy...ughghghg
i usually have trouble with this too. it haven't done much java but one method that might work is to make a variable and draw the animation by what the variable says. like this

Code: Select all

int ani_tracker;
 frame_counter++;
 if(frame_counter > 0 && frame_counter < 20) ani_tacker = 1;
if(frame_counter > 19 &&  frame_counter < 40) ani_tracker = 2;
if(ani_tracker == 1) draw(frame1);
if(ani_tracker == 2) draw(frame2;
// and so on...
this is of course pseudo code and you probably need to adjust the values but it works pretty well.

EDIT: opps i forgot to mention that you need to use a timer to regulate frame_counter so that it will add at the same speed. i learned this method from a c++ tutorial but i cant seem to find it.
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Java Help

Post by Perry »

http://www.loomsoft.net/resources/alltu ... esson7.htm i found that is the tutorial i mentioned in case you haven't figured it out. it isn't a java tutorial but it explains the concept i tried to explain.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Java Help

Post by hallsofvallhalla »

kewl thanks!
User avatar
Perry
Posts: 409
Joined: Tue Jun 23, 2009 4:26 pm

Re: Java Help

Post by Perry »

for me at first it seemed overly drawn out which i think it is for simplicity but you could always make up some kind of function once you get the concept. hope it helps!
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: Java Help

Post by Falken »

kyraneth wrote:don't want to go OT, but could you point me to some easy beginner java tuts? Or any useful resource you have, i'm looking on learning java.
Check out: http://java.sun.com/docs/books/tutorial/
Under "Trails Covering the Basics"
Some quite ok tutorials to get started with JAVA.
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
Post Reply

Return to “Coding”