10-03 02:00:04.624: ERROR/AndroidRuntime(3366): FATAL EXCEPTION: main
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): java.lang.IllegalStateException: Could not execute method of the activity
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.view.View$1.onClick(View.java:2082)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.view.View.performClick(View.java:2461)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.view.View$PerformClick.run(View.java:8890)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.os.Handler.handleCallback(Handler.java:587)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.os.Handler.dispatchMessage(Handler.java:92)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.os.Looper.loop(Looper.java:123)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at java.lang.reflect.Method.invoke(Method.java:521)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at dalvik.system.NativeStart.main(Native Method)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): Caused by: java.lang.reflect.InvocationTargetException
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at com.androidbook.sensor.accelerometer.MainActivity.Click(MainActivity.java:121)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at java.lang.reflect.Method.invoke(Method.java:521)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at android.view.View$1.onClick(View.java:2077)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): ... 11 more
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): Caused by: java.lang.NullPointerException
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at com.androidbook.sensor.accelerometer.MainActivity$2.run(MainActivity.java:101)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): at java.lang.Thread.run(Thread.java:1096)
10-03 02:00:04.624: ERROR/AndroidRuntime(3366): ... 15 more
我正在尝试构建一个Android应用程序,它利用加速度计以设定的时间间隔记录捕获的数据,通过使用Handler并将数据存储在SQLite中。
除此之外,这也是我第一次进行Android编程。在调试时,上面是我得到的,它告诉我我搞砸了。有三个例外,我只是不知道我做错了什么。我跟着各种教程写了这封信,它仍然在我身上崩溃。
在MainActivity的第101行,我正在从我定义的另一个类调用一个函数来处理数据库插入并导出到SD卡,放在Runnable的run()中。 121是run()的Thread执行。即使定义的类中的函数为空,它仍然会崩溃。无论如何,我推测只是执行任何混乱。
任何关于此的智慧都将受到高度赞赏。提前谢谢。
public void Click(View view)
{
final ToggleButton tg = (ToggleButton) findViewById (R.id.Toggle);
Runnable initialize = new Runnable() {
public void run() {
routine.initialize(); <- 101
}
};
Runnable periodicTask = new Runnable() {
public void run() {
routine.insertData();
handler.postDelayed(this, (long) (routine.getInterval()*1000));
}
};
switch (view.getId())
{
case R.id.button1:
{
new Thread(initialize).run(); <- 121
new Thread(periodicTask).start();
tg.setClickable(true);
tg.setChecked(true);
Toast.makeText(this, "Started!", Toast.LENGTH_SHORT).show();
break;
}
case R.id.button2:
{
handler.removeCallbacks(periodicTask);
tg.setClickable(false);
tg.setChecked(false);
Toast.makeText(this, "Stopped!", Toast.LENGTH_SHORT).show();
break;
}
}
请勿在切换按钮上询问我,这是来自我的其他成员。
我还是个新手,所以请原谅编程的原始外观。我对Handler的理解仅限于几个小时的阅读。