Null Pointer Exceprion .MainActivity $ 1.onTick(MainActivity.java:23)

时间:2013-08-05 13:48:24

标签: android

我正在尝试从this webpage实现此代码。

但出于某种原因,我得到了NullPointerException

    08-05 08:59:09.512: E/AndroidRuntime(875): java.lang.NullPointerException
    08-05 08:59:09.512: E/AndroidRuntime(875):  at com.example.basiccountdown.MainActivity$1.onTick(MainActivity.java:23)

代码:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);
    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener(this);

    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval);

    text.setText(text.getText() + String.valueOf(startTime/1000));

    }

更多stacktrace

08-05 09:21:44.313: D/AndroidRuntime(1258): Shutting down VM
08-05 09:21:44.313: W/dalvikvm(1258): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
08-05 09:21:44.412: E/AndroidRuntime(1258): FATAL EXCEPTION: main
08-05 09:21:44.412: E/AndroidRuntime(1258): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.basiccountdown/com.example.basiccountdown.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.basiccountdown.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.basiccountdown-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.basiccountdown-2, /system/lib]]
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.os.Looper.loop(Looper.java:137)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at java.lang.reflect.Method.invokeNative(Native Method)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at java.lang.reflect.Method.invoke(Method.java:525)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at dalvik.system.NativeStart.main(Native Method)
08-05 09:21:44.412: E/AndroidRuntime(1258): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.basiccountdown.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.basiccountdown-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.basiccountdown-2, /system/lib]]
08-05 09:21:44.412: E/AndroidRuntime(1258):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-05 09:21:44.412: E/AndroidRuntime(1258):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
08-05 09:21:44.412: E/AndroidRuntime(1258):     ... 11 more
08-05 09:28:51.782: D/AndroidRuntime(1307): Shutting down VM
08-05 09:28:51.782: W/dalvikvm(1307): threadid=1: thread exiting with uncaught exception (group=0x414c4700)

第23行是super.onCreate(savedInstanceState);

3 个答案:

答案 0 :(得分:1)

将计时器活动类的名称更改为MainActivity。

答案 1 :(得分:1)

MainActivity添加到AndroidManifest。这应该可以解决问题。这是教程,没有说任何地方应该添加此活动。它适用于新项目,这会自动发生。将其添加到清单中:

<activity android:name="com.example.basiccountdown.MainActivity">

然后转到Project - &gt;属性 - &gt;建筑商也是。

答案 2 :(得分:0)

如果您复制了您提到的代码,我可以提出两件事:

1)如果您已正确定义主要活动,请检查清单

2)在你的/res/layout/activity_timer.xml中检查你有id为“timer”的文本视图