gpt4 book ai didi

android - 我在LogCat文件中找不到错误

转载 作者:行者123 更新时间:2023-12-03 17:38:54 35 4
gpt4 key购买 nike

我的程序崩溃了,老实说我不知道​​。我有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

最佳答案

findViewById()仅在创建 View 后才起作用,这在此处的onCreate()中发生:setContentView(R.layout.activity_main);
因此,要解决您的问题,只需将右侧具有findViewById()的所有分配移动到onCreate()行下方的setContentView(R.layout.activity_main);(如已经建议过的Raghunandan)。

关于android - 我在LogCat文件中找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17030683/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com