Sunday, April 20, 2014

AlertDialog calling message mechanism in operation stops unexpectedly!!!!?????




/ / AlertDialog calling message mechanism in operation stops unexpectedly!!!!?????
/ / The following code can be directly run, no solution ah, why stop unexpectedly? ? Help! !

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) ;/ / wait for the UI thread to complete initialization
} Else {
timerMission.schedule (Mission, 4000) ;/ / wait for the UI thread to complete initialization
}

}
public void TipDlgDisaplay (String TipContext, String LeftButtonText, String RightButtonText, int Type)
{
new AlertDialog.Builder (this)
/ /. SetIcon (R.drawable.title)
. SetTitle ("OK?")
. SetMessage ("Are?")
. SetPositiveButton ("No ()",
new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
/ / CancelLoadIN ();
StartW (2);
}
})
. SetNegativeButton ("a ()", new DialogInterface.OnClickListener () {

public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
/ / BackRun ();
StartW (1);
}
})
. Create ()
. Show ();
}



}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
1, I hope to perform other operations after the main interface to update the output after waiting some time, but the interface is not suspended animation,
2, if Thread.sleep (), then the interface will appear suspended animation, so I want to use timer to achieve,

PS: In AlertDialog in use SendMessage to send a message to the main thread throwing the same cause unexpected stop, no solution, throw ourselves messages should not go wrong. . In short, there seems to be a lot of AlertDialog operations will result in unexpected stop, why?
Reply:
public void run ()
{
text.setText ("sad");
}
}; Thread which can not text.setText ("sad"); get a message to update the text

No comments:

Post a Comment