CountDownTimer上的NullPointerExeption

时间:2014-08-10 11:13:13

标签: java android nullpointerexception

在我的CuntdownTimerProject中我遇到了NullPointerExeption的问题。 我无法发现代码中的错误。我还查看了关于NullPointerExeption的不同主题,但他们根本无法帮助我。

这是我的onCreate:

 public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startWorkoutTimer(0);

            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (workoutTimer!=null ){
                    workoutTimer.cancel();

                }
                if (restTimer!=null ){
                    restTimer.cancel();
                }
            }
        });

    }

我这里只使用“null”。 logcat还告诉我NullPointerExeption问题来自onCreate方法。

logcat的:

08-10 13:01:58.254  20626-20626/com.stacktesttimer.app D/dalvikvm﹕ Late-enabling CheckJNI
08-10 13:01:58.314  20626-20626/com.stacktesttimer.app D/AndroidRuntime﹕ Shutting down VM
08-10 13:01:58.314  20626-20626/com.stacktesttimer.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41628ba8)
08-10 13:01:58.324  20626-20626/com.stacktesttimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.stacktesttimer.app, PID: 20626
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stacktesttimer.app/com.stacktesttimer.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.stacktesttimer.app.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
08-10 13:02:30.834  20626-20626/com.stacktesttimer.app I/Process﹕ Sending signal. PID: 20626 SIG: 9
08-10 13:03:10.524  20845-20845/com.stacktesttimer.app D/dalvikvm﹕ Late-enabling CheckJNI
08-10 13:03:10.604  20845-20845/com.stacktesttimer.app D/AndroidRuntime﹕ Shutting down VM
08-10 13:03:10.604  20845-20845/com.stacktesttimer.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41628ba8)
08-10 13:03:10.604  20845-20845/com.stacktesttimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.stacktesttimer.app, PID: 20845
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stacktesttimer.app/com.stacktesttimer.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.stacktesttimer.app.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
08-10 13:04:06.714  20845-20845/com.stacktesttimer.app I/Process﹕ Sending signal. PID: 20845 SIG: 9
08-10 13:11:31.134  21456-21456/com.stacktesttimer.app D/dalvikvm﹕ Late-enabling CheckJNI
08-10 13:11:31.154  21456-21462/com.stacktesttimer.app D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
08-10 13:11:31.224  21456-21456/com.stacktesttimer.app D/AndroidRuntime﹕ Shutting down VM
08-10 13:11:31.224  21456-21456/com.stacktesttimer.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41628ba8)
08-10 13:11:31.224  21456-21456/com.stacktesttimer.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.stacktesttimer.app, PID: 21456
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stacktesttimer.app/com.stacktesttimer.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.stacktesttimer.app.MainActivity.onCreate(MainActivity.java:24)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

提前感谢。

完整代码:

public class MainActivity extends Activity {


    CountDownTimer workoutTimer;
    CountDownTimer restTimer;
    Button btnStart, btnStop;

    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startWorkoutTimer(0);

            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (workoutTimer!=null ){
                    workoutTimer.cancel();

                }
                if (restTimer!=null ){
                    restTimer.cancel();
                }
            }
        });

    }


    public void startWorkoutTimer(final int count ){


        workoutTimer=new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

                if(count!=3){
                    startRestTimer(count);
                }

            }
        };
        workoutTimer.start();

    }


    public void startRestTimer(final int count ){


        restTimer=new CountDownTimer(15000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {

                if(count!=3){
                    startWorkoutTimer(count+1);
                }

            }
        };
        restTimer.start();

    }

1 个答案:

答案 0 :(得分:2)

btnStop和btnStart未初始化

你需要通过

获得它们
btnStart = findViewById(R.id.yourStartbuttonId);