java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)

时间:2017-01-19 21:34:08

标签: java android nullpointerexception

当我按下计时器的开始按钮时出现错误。我不知道为什么它不起作用。之前我曾使用过这段代码,但现在当我尝试运行它时,它会立即崩溃并出现错误。

这是我的计时器代码的类:

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)

1 个答案:

答案 0 :(得分:0)

这只是一个有根据的猜测,但由于您在静态方法wegmansTimerText中调用字段updateTimer上的方法,可能会发生NPE。

通过init方法猜测updateTimer在调用onCreate之前执行,该实例化特定字段。这种方式wegmansTimerText为空。您必须在生命周期中正确地对定时器的被调用方法进行对齐 - 这意味着您可能需要将静态方法的内容放入onCreate或以下生命周期状态。

我现在没有安装Android Studio,因此未经过测试,但可能会指向正确的方向。

希望这有帮助!