我的应用在Android Studio中不断崩溃

时间:2014-12-24 20:55:03

标签: android android-studio

我目前正在开发Android Studio中的计时器应用程序,它在我甚至可以运行它之前一直在崩溃。我需要今晚完成它,这可能是我爸爸的圣诞礼物(这是他的公司)。

这是我的代码:

public class TimerActivity extends ActionBarActivity {
    Button btnStart = (Button) findViewById(R.id.btnStart);
    TextView txtTimer = (TextView) findViewById(R.id.textViewTime);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);
       // btnStart.setBackgroundColor(0x57C173);

        final CountDownTimer timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long secondsRemaining = millisUntilFinished / 60;
                long minutesRemaining = secondsRemaining / 60;
                secondsRemaining = secondsRemaining % 60;

                txtTimer.setText(minutesRemaining + ":" + secondsRemaining);
            }

            @Override
            public void onFinish() {
                txtTimer.setText("You are Awesome!");
            }
        };

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
                btnStart.setText("Cancel");

                btnStart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        timer.cancel();
                        btnStart.setText("Start");
                        txtTimer.setText("1:00");
                    }
                });
            }
        });
    }
}

这就是我的Logcat所说的:

12-24 14:48:01.324  16878-16878/com.mintemovement.minutemovement W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416a9e30)
12-24 14:48:01.334  16878-16878/com.mintemovement.minutemovement E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mintemovement.minutemovement, PID: 16878
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mintemovement.minutemovement/com.mintemovement.minutemovement.TimerActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2357)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$800(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5633)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.app.Activity.findViewById(Activity.java:1952)
            at com.mintemovement.minutemovement.TimerActivity.<init>(TimerActivity.java:12)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1208)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
            at android.app.ActivityThread.access$800(ActivityThread.java:153)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5633)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

试试这个

public class TimerActivity extends ActionBarActivity {

Button btnStart;
TextView txtTimer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);
   // btnStart.setBackgroundColor(0x57C173);
   btnStart = (Button) findViewById(R.id.btnStart);
   txtTimer = (TextView) findViewById(R.id.textViewTime);