BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #5536同步于 2012/3/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖

问题是这样的。

EastDon
2012/3/15镜像同步8 回复
问题是这样的。 做了个倒计时器。 刚刚学安卓。。相当不懂。。 倒计时的方法是用Handler每隔1s给自己发送个消息,然后处理。 现在我想弹出个及其简单的对话框。。在时间到的时候。。但是总是出错。。 是不是Handler里面不能添加对话框呢? 还是要XML的组件定义呢? 我看网上的教程不用新弄个XML啊。。 开始粘代码把。。 public class DaojishiActivity extends Activity { /** Called when the activity is first created. */ int i=10; int inint; int fen; int miao; Handler myhandler=null; Message msg=null; AlertDialog.Builder mdialog; public void dialog(){ mdialog.setTitle("时间到~"); mdialog.setPositiveButton("OK", null); mdialog.setMessage("Times up!!!").show(); } void clock(){ myhandler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what){ case 100: TextView a =(TextView)findViewById(R.id.textView1); if(fen==0&miao==1){ a.setText("时间到了哦~!"); //dialog(); }else if(miao==0&fen!=0){ fen--; miao=59; a.setText("还剩"+fen+"分"+miao+"秒"); }else{ miao--; a.setText("还剩"+fen+"分"+miao+"秒"); } sendMessageDelayed(obtainMessage(100),1000); break; } } }; } logcat需要么? 算了还是放上来把。。 话说还有。。logcat的错误信息怎么读呢?如果每次有问题就可以自己解决了
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
he1l0机器人#1 · 2012/3/15
TextView a =(TextView)findViewById(R.id.textView1); 就这行能产这个异常,判断一下a是不是null吧 另外两个boolean and用&&,楼主这么写能编译过?
EastDon机器人#2 · 2012/3/15
能编译。。。 我代码的30行不是被我注释掉了么? 注释掉以后能编译能运行倒计时部分。。但是只要一到时间到了提醒部分就会弹出错误。。[ema8] 【 在 he1l0 的大作中提到: 】 : TextView a =(TextView)findViewById(R.id.textView1); : 就这行能产这个异常,判断一下a是不是null吧 : 另外两个boolean and用&&,楼主这么写能编译过?
EastDon机器人#3 · 2012/3/16
..
feiyunruyue机器人#4 · 2012/3/16
Activity 可以没有onCreate 方法吗?我咋没找到
EastDon机器人#5 · 2012/3/17
额。。这是部分代码的说。。。 看来还是把所以源代码贴上来比较好。。。 package wdm.android.test.daojishi; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class DaojishiActivity extends Activity { /** Called when the activity is first created. */ int i=10; int inint; int fen; int miao; Handler myhandler=null; Message msg=null; AlertDialog.Builder mdialog; public void dialog(){ mdialog.setTitle("时间到~"); mdialog.setPositiveButton("OK", null); mdialog.setMessage("Times up!!!").show(); } void clock(){ myhandler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what){ case 100: TextView a =(TextView)findViewById(R.id.textView1); if(fen==0&miao==1){ a.setText("时间到了哦~!"); //dialog(); }else if(miao==0&fen!=0){ fen--; miao=59; a.setText("还剩"+fen+"分"+miao+"秒"); }else{ miao--; a.setText("还剩"+fen+"分"+miao+"秒"); } sendMessageDelayed(obtainMessage(100),1000); break; } } }; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText input = (EditText)findViewById(R.id.input); final EditText input2 = (EditText)findViewById(R.id.input2); final TextView a =(TextView)findViewById(R.id.textView1); Button button = (Button)findViewById(R.id.button); OnClickListener buttonlis= new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub fen=Integer.valueOf(input.getText().toString()); miao=Integer.valueOf(input2.getText().toString()); a.setText("还剩"+fen+"分"+miao+"秒"); clock(); if(myhandler!=null) msg = myhandler.obtainMessage(100); if(msg!=null) myhandler.sendMessageDelayed(msg, 1000); } }; //设置按钮事件监听 button.setOnClickListener(buttonlis); } } 【 在 feiyunruyue 的大作中提到: 】 : Activity 可以没有onCreate 方法吗?我咋没找到
qingyiquan机器人#6 · 2012/3/17
你的程序有两个问题: 1.mdialog对象未初始化,所以会造成空指针异常错误。 2.myhandler的定义有问题,当时间倒计时到0:1时候,将会一直循环下去,你记录的时间一直是0:1,这样是死循环,很不好。 解决办法: 1.添加初始化语句: public void dialog(){ mdialog = new AlertDialog.Builder(DaojishiActivity.this); mdialog.setTitle("时间到~"); mdialog.setPositiveButton("OK", null); mdialog.setMessage("Times up!!!").show(); } 2.handler这样定义吧,当倒计时终止的时候就不要再发消息了 switch(msg.what){ case 100: TextView a =(TextView)findViewById(R.id.textView1); if(fen==0&miao==1){ a.setText("时间到了哦~!"); dialog(); } else if(miao==0&fen!=0) { fen--; miao=59; a.setText("还剩"+fen+"分"+miao+"秒"); sendMessageDelayed(obtainMessage(100),1000); }else{ miao--; a.setText("还剩"+fen+"分"+miao+"秒"); sendMessageDelayed(obtainMessage(100),1000); } break; } 【 在 EastDon 的大作中提到: 】 : [size=5][face=黑体]问题是这样的。 : 做了个倒计时器。 : 刚刚学安卓。。相当不懂。。 : ...................
qingyiquan机器人#7 · 2012/3/17
@EastDon 希望能够帮到你
EastDon机器人#8 · 2012/3/18
成功解决了! 非常感谢啊啊啊~ 【 在 qingyiquan 的大作中提到: 】 : @EastDon 希望能够帮到你