• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Declaring Items Causes Crashes

VaMoose

Lurker
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
findViewById(R.id.theid);
When I dont declare it ,ie: I DONT put
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
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:
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:

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
 
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! :D:D
 
I have re-written the code and now have:
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 :
score.setText(points);

if I comment out just that line, the app works, but doesnt do anything.

Any Ideas?
 
Back
Top Bottom