-2
當我點擊我的定時器的開始按鈕時出現錯誤。我不知道爲什麼它不工作。我以前使用過這段代碼,但現在當我嘗試運行它時,它立即崩潰並顯示錯誤。java.lang.NullPointerException:試圖調用虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)
這裏是我的計時器代碼的類:
package com.example.pc.mealtimers;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
public class WegmansActivity extends AppCompatActivity{
static TextView wegmansTimerText;
static Boolean counterIsActive = false;
Button wegStart;
static CountDownTimer countDownTimer;
public static void resetTimer(){
wegmansTimerText.setText("16:00");
countDownTimer.cancel();
counterIsActive = false;
}
public static void wegmansTimerInit() {
if (counterIsActive == false) {
counterIsActive = true;
//wegButton.setText("Stop!");
countDownTimer = new CountDownTimer(960000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
updateTimer((int) millisUntilFinished/1000);
}
@Override
public void onFinish() {
wegmansTimerText.setText("0:00");
}
}.start();
}else{
wegmansTimerText.setText("16:00");
resetTimer();
}
}
public static void updateTimer(int secondsLeft){
//System.out.println("TICKING");
counterIsActive = true;
int minutes = (int) secondsLeft/60;
int seconds = secondsLeft - minutes *60;
String secondString = Integer.toString(seconds);
if (seconds <=9){
secondString = "0" + secondString;
}
wegmansTimerText.setText(Integer.toString(minutes) + ":" + secondString);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wegmans);
wegmansTimerText = (TextView)findViewById(R.id.wegmansTimerText);
wegStart = (Button)findViewById(R.id.startButton);
}
}
這裏是我在計時器位於佈局代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".WegmansActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/backButton"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="backButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="400°"
android:id="@+id/degreeText"
android:textSize="60sp"
android:layout_below="@+id/backButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="16:00"
android:id="@+id/wegmansTimerText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="60sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/startButton"
android:layout_marginBottom="52dp"
android:textSize="20sp"
android:padding="20dp"
android:onClick="wegmansTimer"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我的錯誤::
java.lang.IllegalArgumentException: Binary XML file line #44: Process: com.example.pc.mealtimers, PID: 2346
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.pc.mealtimers.WegmansActivity.updateTimer(WegmansActivity.java:60)
at com.example.pc.mealtimers.WegmansActivity$1.onTick(WegmansActivity.java:33)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在哪個lifecyle步驟中你調用你的函數? –