如何从主要活动之外做一个祝酒词

时间:2011-07-11 01:30:14

标签: android

我试图在主要活动之外展示吐司并且它崩溃了。

public class TCPConnection implements NetworkInterface{

    private final static String TAG = "TCPConnection";
    private final static String IP = "1.1.1.1";
    private final static String PORT = "12001";

    public boolean onDataSend(Work work){

        boolean sent = false;

        if(mRunning){
            try {
                //Log.i(TAG, "onDataSend!");
                mOut.write(work.getbData());
                sent = true;

            } catch (IOException e) {
                sent = false;
                mDelegate.setRun(false);
                e.printStackTrace();

                Toast.makeText(mContext, "Connection has been lost", Toast.LENGTH_LONG).show();             

                try {
                    stop();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }   
        }
    }

我使用getApplicationContext();

传递Context

如何从主要活动之外创建祝酒词?

2 个答案:

答案 0 :(得分:3)

试试这个:

public boolean onDataSend(Work work, Context mcontext){
    ........
    Toast.makeText(mContext, "Connection has been lost", Toast.LENGTH_LONG).show();
    .......

调用方法时:

    onDataSend(work,MainActivity.this.getApplicationContext())
    ........

答案 1 :(得分:0)

使用getApplicationContext()创建一个toast /显示带有上下文的对话框会导致崩溃。

请参阅Android: ProgressDialog.show() crashes with getApplicationContext