倒计时器运行时错误

时间:2017-11-26 18:18:52

标签: java android nullpointerexception runtime-error countdowntimer

这里有新手,我尝试制作简单的Android应用程序。 要进入睡眠时间,请使用三个按钮"睡眠","醒来","重置"。 当我在手机中跑步时,它会说"不幸的是,已停止"。

我已经在调试模式下运行它,但它也不能正常工作。

这里是我的java代码:

private void saveAppDatabase() {
    try {
        File sd = Environment.getExternalStorageDirectory();

        if (sd.canWrite()) {
            String currentDBPath =
                    getDatabasePath("AppDatabase").getAbsolutePath();
            String backupDBPath = "AppDatabase.db";
            File currentDB = new File(currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我的XML文件:

package com.example.android.sleepreminder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {

    Button buttonSleep = (Button) findViewById(R.id.sleep);
    Button buttonWoke = (Button) findViewById(R.id.woke);
    Button buttonReset = (Button) findViewById(R.id.reset);
    EditText text1 = (EditText) findViewById(R.id.count_down_timer);
   // private static final String FORMAT = "%02d:%02d:%02d";

    int second =60;

    public CountDownTimer countDownTimer = new CountDownTimer(second * 10000, 1000) {
        @Override
        public void onTick(long millis) {
            text1.setText((int)millis * 1000);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onFinish() {
            text1.setText("reset");
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonSleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              countDownTimer.start();

            }});

        buttonWoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }});


        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                countDownTimer.onFinish();

            }});


    }

}

这就是我在日志中得到的错误:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/count_down_timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="time" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <Button
        android:id="@+id/sleep"
        style="@style/Button"
        android:text="Sleep" />

    <Button
        android:id="@+id/woke"
        style="@style/Button"
        android:text="Woke" />

    <Button
        android:id="@+id/reset"
        style="@style/Button"
        android:text="Reset" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

你能说出我的问题的具体内容吗?

1 个答案:

答案 0 :(得分:0)

您需要在findViewById方法之后使用setContentView,否则您将获得NPE,因为视图尚未膨胀。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonSleep = (Button) findViewById(R.id.sleep);
    Button buttonWoke = (Button) findViewById(R.id.woke);
    Button buttonReset = (Button) findViewById(R.id.reset);
    EditText text1 = (EditText) findViewById(R.id.count_down_timer);

    buttonSleep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          countDownTimer.start();

        }});

    buttonWoke.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        }});


    buttonReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            countDownTimer.onFinish();

        }});


}