我该如何敬酒从主要活动外 [英] How do I make a toast from outside of the main activity

查看:100
本文介绍了我该如何敬酒从主要活动外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想展现的主要活动以外的烤面包和坠毁。

I was trying to show a toast outside of the main activity and it crashed.

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()通过上下文;

您如何可以从主活动之外创建一个面包?

How can you create a toast from outside of the main activity?

推荐答案

试试这个:

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

当你调用方法:

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

这篇关于我该如何敬酒从主要活动外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆