Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

test: Reply
 
LinkBack Thread Tools
Old December 7th, 2012, 01:46 PM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Dec 2012
Location: Bryanston, Gauteng, South Africa
Gender: Male
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Cry Declaring Items Causes Crashes

Hi There. I am trying to build a game called 'spam' where you hit the button as much as possible in the given time. however, in the game class, it will run fine if I dont declare the actual spam button and the textviews that I want to change (score and time left).

I am using
Quote:
findViewById(R.id.theid);
When I dont declare it ,ie: I DONT put
Quote:
spambutton = (Button) findViewById(R.id.gameSpamButton);
, it runs fine - it is just static and wont do anything.

But as soon as I try declare anything (the spam button or the time left text view or the score text view), when I try to start the activity (via the Intent), it says that
Quote:
the application test (com.example.test) has stopped unexpectedly. Please try again
I am using Eclipse Version: 3.7.1 Build id: M20110909-1335.
I am running Windows 7 Home Premium 64Bit, 8GB RAM, 1GB GDDR5 GTS 450 GeForce graphics card, Asus P8 motherboard, 2TB Samsung HDD, 500GB Samsung HDD, standard 16x optical drive, 5 fans, quad core i5 3.10GHz CPU.

This is my code for the class so far, and it crashes with the above error:
Quote:
package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Game extends Activity{

Button spamme;
TextView timeleft, score;
public int points = 0;
public int finalscore = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);

//declaration of items
spamme = (Button) findViewById(R.id.gameSpamButton);
timeleft = (TextView) findViewById(R.id.gameTimeLeftTextView);
score = (Button) findViewById(R.id.gameScoreTextView);

//onClick events


//threads
}

}
However, if I comment out the declarations then it works fine, shown below:

Quote:
package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Game extends Activity{

Button spamme;
TextView timeleft, score;
public int points = 0;
public int finalscore = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);

//declaration of items
//spamme = (Button) findViewById(R.id.gameSpamButton);
//timeleft = (TextView) findViewById(R.id.gameTimeLeftTextView);
//score = (Button) findViewById(R.id.gameScoreTextView);


//onClick events


//threads
}

}
Please help! it is driving me insane!

Thanks,
Josh

Advertisements
VaMoose is offline  
Reply With Quote
sponsored links
Old December 7th, 2012, 01:49 PM   #2 (permalink)
The Real Bass Creator
 
Digital Controller's Avatar
 
Join Date: Sep 2012
Location: Cincinnati, Ohio
Gender: Male
Posts: 12,497
 
Device(s): Galaxy Note 3 4.4.2(Eclipse) , Nexus 7 (Stock), (RIP) Galaxy Nexus 4.4(SlimKat)
Carrier: Verizon

Thanks: 748
Thanked 3,420 Times in 2,163 Posts
Default

Hey Josh i have moved this thread into the Application Development forum so you receive some more assistance. I hope this helps, and welcome to the forums man!
__________________
Want Faster help? Read this before posting!

I love this community and so should you! So before posting please read:
Site Rules & Guidelines and Android FAQ's
Digital Controller is online now  
Reply With Quote
The Following User Says Thank You to Digital Controller For This Useful Post:
VaMoose (December 9th, 2012)
Old December 9th, 2012, 04:22 AM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Dec 2012
Location: Bryanston, Gauteng, South Africa
Gender: Male
Posts: 5
 
Device(s):
Carrier: Not Provided

Thanks: 1
Thanked 0 Times in 0 Posts
Default

I have re-written the code and now have:
Quote:
package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Game extends Activity{

Button spam;
TextView timeleft, score;
public int points = 0;
public int finalscore = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);

spam = (Button) findViewById(R.id.gameSpamButton);
timeleft = (TextView) findViewById(R.id.gameTimeLeftTextView);
score = (TextView) findViewById(R.id.gameScoreTextView);

spam.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
points++;
score.setText(points);
}
});
}
}
However it still crashes. I have determined that the line causing the crash is :
Quote:
score.setText(points);
if I comment out just that line, the app works, but doesnt do anything.

Any Ideas?
VaMoose is offline  
Last edited by VaMoose; December 9th, 2012 at 04:25 AM.
Reply With Quote
Old December 10th, 2012, 08:41 AM   #4 (permalink)
Junior Member
 
Join Date: May 2012
Posts: 27
 
Device(s): Nexus 7 (Android 4.2.1), Samsung Galaxy Mini (Android 2.3.6)
Carrier: Not Provided

Thanks: 1
Thanked 2 Times in 2 Posts
Default

I have already answered it in Button onClick setText causes Crash ?
myrecek is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 03:05 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.