我的程序崩溃了,老实说我不知道。我有logcat错误,我在那里找不到它。任何帮助将不胜感激。我收到一个空指针异常,但我不知道它会发生在哪里。
package com.example.puremmacompetitionjiujitsuscorer;
//~--- non-JDK imports --------------------------------------------------------
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity { // Starts the main activity
private int blueScore = 0;
private int redScore = 0;
String flag = "";
Button start = (Button) findViewById(R.id.play);
Button stop = (Button) findViewById(R.id.stop);
final ImageButton reset = (ImageButton) findViewById(R.id.resetButton1);
final ImageButton blue = (ImageButton) findViewById(R.id.imageButton9);
final ImageButton red = (ImageButton) findViewById(R.id.imageButton10);
final ImageButton sweep = (ImageButton) findViewById(R.id.imageButton5);
final ImageButton pass = (ImageButton) findViewById(R.id.imageButton8);
final ImageButton mount = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton backMount = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton kneeOnBelly = (ImageButton) findViewById(R.id.imageButton7);
final ImageButton takeDown = (ImageButton) findViewById(R.id.imageButton6);
final TextView blueScoreCount = (TextView) findViewById(R.id.blueScore1);
final TextView redScoreCount = (TextView) findViewById(R.id.redScore1);
TextView timerOnScreen = (TextView) findViewById(R.id.timer);
EditText editTimerOnScreen = (EditText) findViewById(R.id.editTimer);
private CountDownTimer countDownTimer;
private long totalTimeCountInMilliseconds;
private long timeBlinkInMilliseconds;
private boolean blink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.play);
stop = (Button) findViewById(R.id.stop);
timerOnScreen = (TextView) findViewById(R.id.timer);
editTimerOnScreen = (EditText) findViewById(R.id.editTimer);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.stop) {
timerOnScreen.setTextAppearance(getApplicationContext(), R.style.normalText);
setTimer();
stop.setVisibility(View.VISIBLE);
start.setVisibility(View.GONE);
editTimerOnScreen.setVisibility(View.GONE);
editTimerOnScreen.setText("");
startTimer();
} else if (v.getId() == R.id.play) {
countDownTimer.cancel();
start.setVisibility(View.VISIBLE);
stop.setVisibility(View.GONE);
editTimerOnScreen.setVisibility(View.VISIBLE);
}
}
private void setTimer() {
int time = 0;
if (!editTimerOnScreen.getText().toString().equals("")) {
time = Integer.parseInt(editTimerOnScreen.getText().toString());
} else {
Toast.makeText(MainActivity.this, "Please Enter Minutes...", Toast.LENGTH_LONG).show();
}
totalTimeCountInMilliseconds = 60 * time * 1000;
timeBlinkInMilliseconds = 30 * 1000;
}
private void startTimer() {
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
@Override
public void onTick(long leftTimeInMilliseconds) {
int seconds = (int) (leftTimeInMilliseconds / 1000);
if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
timerOnScreen.setTextAppearance(getApplicationContext(), R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink) {
timerOnScreen.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
timerOnScreen.setVisibility(View.INVISIBLE);
}
blink = !blink; // toggle the value of blink
}
timerOnScreen.setText(String.format("%02d:%02d:%02d", seconds / 3600,
(seconds % 3600) / 60, (seconds % 60)));
// format the textview to show the easily readable format
}
@Override
public void onFinish() {
// this function will be called when the timecount is finished
timerOnScreen.setText("Time up!");
timerOnScreen.setVisibility(View.VISIBLE);
start.setVisibility(View.VISIBLE);
stop.setVisibility(View.GONE);
editTimerOnScreen.setVisibility(View.VISIBLE);
}
}.start();
}
});
reset.setOnClickListener(new View.OnClickListener() { // resets the score values in the top corners back to 0
public void onClick(View u) {
redScore = 0;
redScoreCount.setText("" + redScore);
blueScore = 0;
blueScoreCount.setText("" + blueScore);
}
});
blue.setOnClickListener(new View.OnClickListener() { // assigns the value of flag to the blueScore when the blue button is clicked
public void onClick(View g) {
flag = "blue";
}
});
red.setOnClickListener(new View.OnClickListener() { // assigns the value of flag to the redScore when the red button is clicked
public void onClick(View m) {
flag = "red";
}
});
takeDown.setOnClickListener(new View.OnClickListener() { // adds 2 points to either the blue or red score depending on which flag was hit last
public void onClick(View v) {
if (!flag.equals("")) {
;
}
{
if ("red".equals(flag)) {
redScore += 2;
redScoreCount.setText("" + redScore);
} else {
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
}
}
});
sweep.setOnClickListener(new View.OnClickListener() { // adds 2 points to either the blue or red score depending on which flag was hit last
public void onClick(View w) {
if (!flag.equals("")) {
;
}
{
if ("red".equals(flag)) {
redScore += 2;
redScoreCount.setText("" + redScore);
} else {
blueScore += 2;
}
blueScoreCount.setText("" + blueScore);
}
}
});
pass.setOnClickListener(new View.OnClickListener() { // adds 3 points to either the blue or red score depending on which flag was hit last
public void onClick(View q) {
if (!flag.equals("")) {
;
}
{
if ("red".equals(flag)) {
redScore += 3;
redScoreCount.setText("" + redScore);
} else {
blueScore += 3;
}
blueScoreCount.setText("" + blueScore);
}
}
});
mount.setOnClickListener(new View.OnClickListener() { // adds 4 points to either the blue or red score depending on which flag was hit last
public void onClick(View t) {
if (!flag.equals("")) {
;
}
{
if ("red".equals(flag)) {
redScore += 4;
redScoreCount.setText("" + redScore);
} else {
blueScore += 4;
}
blueScoreCount.setText("" + blueScore);
}
}
});
backMount.setOnClickListener(new View.OnClickListener() { // adds 4 points to either the blue or red score depending on which flag was hit last
public void onClick(View s) {
if (!flag.equals("")) {
;
}
{
if ("red".equals(flag)) {
redScore += 4;
redScoreCount.setText("" + redScore);
} else {
blueScore += 4;
}
blueScoreCount.setText("" + blueScore);
}
}
});
kneeOnBelly.setOnClickListener(new View.OnClickListener() { // adds 2 points to either the blue or red score depending on which flag was hit last
public void onClick(View g) {
if (!flag.equals("")) {
;
}
{
if ("red".equals(flag)) {
redScore += 2;
redScoreCount.setText("" + redScore);
} else {
blueScore += 2;
}
blueScoreCount.setText("" + blueScore);
}
}
});
}
public boolean onCreateOptionsMenu(Menu menu) { // I have no idea what this does. It's just automatically generated
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//〜由Jindent --- http://www.jindent.com
格式化答案 0 :(得分:0)
findViewById()
仅在创建视图后才有效,这在onCreate()
此处发生:setContentView(R.layout.activity_main);
因此,要解决您的问题,您只需将右侧findViewById()
的所有作业移至行onCreate()
下方setContentView(R.layout.activity_main);
(如Raghunandan已建议的那样)。< / p>