2014-05-11 37 views
0

我从main_activity在进行此调用吐司里面OnPostExecute()不工作

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未显示。任何人都能发现任何东西吗

+0

Toast.makeText(this,result,Toast.LENGTH_SHORT).show();或者Toast.makeText(classname.this,result,Toast.LENGTH_SHORT).show();或者Context con = this; //初始化这个 – Meghna

+0

你确定任务结束时活动还活着吗? –

+0

而不是v.getContext()尝试Register_activity.this – greenapps

回答

0

您可以将Toast消息放入执行runOnUI()线程的内部,以便在正确的应用程序线程中显示警报,试试它并将工作。问候