-1

我正在尝试从此网页实现此代码。

但由于某种原因,我得到了一个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));

    }

更多堆栈跟踪

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

4

3 回答 3

1

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

于 2013-08-05T13:58:24.233 回答
1

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

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

然后也转到项目-> 属性-> 构建器。

于 2013-08-05T14:18:07.923 回答
0

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

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

2) 检查您的 /res/layout/activity_timer.xml 是否有 ID 为“timer”的文本视图

于 2013-08-05T14:25:35.610 回答