我正在编写一个简单的 android 应用程序来演示 wait() 和 notify() 方法。该应用程序包含 1 个按钮和 1 个 textView,textView 将显示一个计时器,而按钮将显示一个包含 2 个按钮的对话框。现在我想做的是每当我按下对话框按钮时,时间暂停,每当我通过按下对话框中的恢复按钮恢复时,时间继续等等。
这是我的代码:
public class MainActivity extends Activity implements OnClickListener{
Timer t;
int count = 0;
Button b;
TextView tv;
Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.textView1);
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
tv.setText(String.valueOf(count));
count++;
}
});
}
}, 1000, 1000);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
synchronized (MainActivity.this)
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
final Dialog d = new Dialog(this);
d.setTitle("Pause Menu");
d.setContentView(R.layout.pausemenu);
Button b1 = (Button) d.findViewById(R.id.Resume);
Button b2 = (Button) d.findViewById(R.id.Main);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.cancel();
thread = new Thread()
{
public void run()
{
synchronized (MainActivity.this) {
try {
wait(1000);
}catch (Exception e) {
// TODO: handle exception
}
notify();
}
}
};
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.cancel();
}
});
d.show();
}
}
这是logcat:
06-09 08:07:55.066: E/AndroidRuntime(635): FATAL EXCEPTION: main
06-09 08:14:43.946: E/AndroidRuntime(664): FATAL EXCEPTION: main
06-09 08:14:43.946: E/AndroidRuntime(664): java.lang.NullPointerException
06-09 08:14:43.946: E/AndroidRuntime(664):
at com.example.timerwithnotify.MainActivity.onClick(MainActivity.java:58)
06-09 08:14:43.946: E/AndroidRuntime(664):
at android.view.View.performClick(View.java:2408)
06-09 08:14:43.946: E/AndroidRuntime(664):
at android.view.View$PerformClick.run(View.java:8816)
06-09 08:14:43.946: E/AndroidRuntime(664):
at android.os.Handler.handleCallback(Handler.java:587)
06-09 08:14:43.946: E/AndroidRuntime(664):
at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 08:14:43.946: E/AndroidRuntime(664): at android.os.Looper.loop(Looper.java:123)
06-09 08:14:43.946: E/AndroidRuntime(664):
at android.app.ActivityThread.main(ActivityThread.java:4627)
06-09 08:14:43.946: E/AndroidRuntime(664):
at java.lang.reflect.Method.invokeNative(Native Method)
06-09 08:14:43.946: E/AndroidRuntime(664):
at java.lang.reflect.Method.invoke(Method.java:521)
06-09 08:14:43.946: E/AndroidRuntime(664):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-09 08:14:43.946: E/AndroidRuntime(664):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
请用代码告诉我如何解决这个问题,我还是个菜鸟:D。感谢您的时间和考虑。