简体   繁体   English

敬酒不显示在asynctask中

[英]Toast in not showing in asynctask

I am trying to get message from the server to show in toast but it does not appear. 我正在尝试从服务器获取消息以显示在吐司中,但它没有出现。 The client receives the message from the server without any errors.I have tried opening UI thread in onpost but it didn't work 客户端从服务器接收到的消息没有任何错误。我尝试在onpost中打开UI线程,但是没有用

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    new test().execute();

}
public class test extends AsyncTask<String,String,String>{


    @Override
    protected String doInBackground(String... params) {

        try
        {
            socket = new Socket("ip", port);
            OutputStream outToServer = socket.getOutputStream();
            DataOutputStream out = new DataOutputStream(outToServer);
            Log.i(debugString, "Connected_reg!");
            out.writeUTF("3");


            InputStream inFromServer = socket.getInputStream();
            DataInputStream in = new DataInputStream(inFromServer);
            Log.i(debugString, in.readUTF());
            string= in.readUTF();

        }
        catch (Exception e) {
            Log.e(debugString, e.getMessage());
        }


        return null;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {

       //super.onPostExecute(s);

                Context context = getApplicationContext();
                CharSequence text = string;
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
    }


}

It might be to do with the context. 这可能与上下文有关。

I've had issues in the past with getApplicationContext not working for certain things, although can't remember what form the top of my head. 过去我遇到过getApplicationContext不适用于某些问题的问题,尽管不记得是什么原因引起的。

Instead of using getApplicationContext, in the activity where you call your async task put this in the constructor call. 除了使用getApplicationContext,在调用活动的异步任务,把this在构造函数调用。 For example, assuming you are going from MainActivity change the line new test().execute(); 例如,假设您要从MainActivity更改行new test().execute(); to new test(MainActivity.this).execute(); 到新test(MainActivity.this).execute();

Then in the async class create the constructor as 然后在异步类中创建构造函数为

public test(Context context) and set a global class variable to be the value of context, and then use this in your toast.makeText instead of what is returned from getApplicationContext. public test(Context context) ,并将全局类变量设置为context的值,然后在toast.makeText中使用它,而不是从getApplicationContext返回的值。

Also take a look in the logcat, see if there are any errors or exceptions being thrown, it might also be worth adding a log line in the onpostexecute method just to double check you're definetely going in there. 还要查看一下logcat,看看是否有任何错误或异常被抛出,可能还值得在onpostexecute方法中添加一条日志行,以再次检查您是否确实要进入该行。

Create a constructor in the test class which receive a context and use that context in the Toast.makeText. 测试类中创建一个接收上下文的构造函数,并在Toast.makeText中使用该上下文。 Pass the host activity context to that constructor. 将主机活动上下文传递给该构造函数。

getApplicationContext() is a Context class method, AsyncTask does not inherent from that class. getApplicationContext()是Context类的方法,AsyncTask并非该类固有的。 I suppose you are in a scope where you can invoke that method but that scope context is not valid in the scope that you are invoking the Toast.makeText method. 我想您处于一个可以调用该方法的范围内,但是该范围上下文在您调用Toast.makeText方法的范围内无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM