OnPostExecute()内的Toast无法正常工作

时间:2014-05-11 08:35:19

标签: android android-asynctask android-toast

我正在从main_activity

进行此调用
Intent createAccount = new Intent(mainact_Context,Register_activity.class);
startActivity(createAccount);

这是调用AsyncTask

的活动
public class Register_activity extends Activity {
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_activity);
        Button RegButton = (Button) this.findViewById(R.id.Regbutton);

        RegButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                RegisterWithServer regser = new RegisterWithServer(context);
                regser.execute();

            }

        });

        setupActionBar();
    }
}

在我的异步活动中,我有这个:

public class RegisterWithServer extends AsyncTask <String,Void,String> {

    Context con;

    public RegisterWithServer(Context context)
    {
        con = context;
    }

    protected String doInBackground(String... params) {

        //this code gets executed perfectly
    }

    @Override
    protected void onPostExecute(String result)
    {
        /*I tried doing something with the context and the result string here and it worked too.*/
        Toast.makeText(con, result , Toast.LENGTH_SHORT).show();
    }
}

doInBackground()部分完美执行。调用onPostExecute()部分,但Toast未显示。谁能发现任何东西?

1 个答案:

答案 0 :(得分:0)

你可以把你的Toast消息放在实现runOnUI()线程的内部,以便在正确的应用程序线程中显示警告,尝试并且可以工作。此致