gpt4 book ai didi

android - 我的应用程序在 Android Studio 中不断崩溃

转载 作者:行者123 更新时间:2023-11-29 01:36:28 29 4
gpt4 key购买 nike

我目前正在 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)

最佳答案

试试这个

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);

关于android - 我的应用程序在 Android Studio 中不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27642083/

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