//AlertDialog中执行调用消息机制操作意外停止!!!!?????
//下面的代码直接可以运行的,无解啊,为什么会意外停止??求救!!package com.example.alertdlgtest;import java.util.Timer;
import java.util.TimerTask;import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends Activity {
Handler mHandler =null;
TextView text;
Timer timerMission;
TimerTask Mission;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
text=(TextView)findViewById(R.id.textView1);
button1.setOnClickListener(new Button.OnClickListener(){
@Override 
public void onClick(View v) {
TipDlgDisaplay(null,null,null,3);
}

});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void StartW(int type)
{
        Mission=new TimerTask(){
         public void run()
         {
text.setText("sad");
         }
        };
timerMission = new Timer();
if(type==1)
{
timerMission.schedule(Mission, 1000);//等待UI线程完成初始化
}else{
timerMission.schedule(Mission, 4000);//等待UI线程完成初始化
} }
public void TipDlgDisaplay(String TipContext,String LeftButtonText,String RightButtonText,int Type)
{
   new AlertDialog.Builder(this)
     //.setIcon(R.drawable.title)
     .setTitle("确定?")
     .setMessage("是否?")
     .setPositiveButton("否()",
       new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog,int which) {
         //CancelLoadIN();
         StartW(2);
        }
       })
       .setNegativeButton("是()", new DialogInterface.OnClickListener(){       public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       //BackRun();
       StartW(1);
      }
})     
.create()
    .show();
}

}

解决方案 »

  1.   

    1、我希望是 主界面更新输出后等待一定时间后执行其他操作,但界面不假死,
    2、如果用Thread.sleep()的话,界面会出现假死,所以我想用timer来实现,PS:在AlertDialog里用SendMessage给主线程抛送消息一样会导致意外停止,无解,自己给自己抛消息,应该不会出问题的。。总之在AlertDialog里好像很多操作都会导致意外停止的,为什么?
      

  2.   

    public void run()
             {
    text.setText("sad");
             }
            };线程里面不能text.setText("sad");得发消息来更新text