getContext(),getApplicationContext(),getBaseContext在Android

时间:2016-08-24 06:24:40

标签: android toast android-toast

我想显示toast条消息,但getContext()中的Toast.makeText((getContext()," Message" , Toat.LENGTH_LONG.show()))发出错误

  

无法解析方法。

问题是我要在哪个类中显示Toast消息不是MainActivity类。这是AsyncTask类。我可以在其他类(Toast类除外)中显示MainActivity消息作为上述问题吗?

import android.os.AsyncTask;     import android.widget.Toast;

public class myClass extends AsyncTask<String, String, String> {

public myClass(double a, double b,Context context ) {
    this.a = a;
    this.b=b;
    this.context = context;
}


protected String doInBackground(String... params) {
        return null;
    }

    protected void onPostExecute(String result) {
               Toast.makeText((getApplicationContext(), "Message", Toast.LENGTH_LONG).show();

    }
}

修改 我创建了构造函数(参见上面的代码),但在MainActivity类我以这种方式调用myClassObj = new myClass(a, b,this);但是给出了错误

  myclass中的myclas()不能应用于:   预计实际   参数参数   a:双倍a   b:双b   context:android.content.Context this(anonymous ... view.View.OnClickListener)

EDIT3

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    myClass Object;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double age = 16;
                double number = 33;
                Object = new myClass(age,number,this);
            }
        });
    }


}

二等。

    import android.content.Context;
    import android.os.AsyncTask;
    import android.widget.Toast;

    public class myClass extends AsyncTask<String, String, String> {

        Context context;
        double a;
        double b;
        public myClass(double a, double b,Context context ) {
            this.a = a;
            this.b=b;
            this.context = context;
        }


        protected String doInBackground(String... params) {
            return null;
        }

        protected void onPostExecute(String result) {
            Toast.makeText((context), "Message", Toast.LENGTH_LONG).show();
        }
    }

3 个答案:

答案 0 :(得分:2)

当您使用this时,它指的是封闭类。在您的情况下,这是View.OnClickListener。但是你需要传递你的Activity的上下文。

所以你需要这样称呼它,

Object = new myClass(age,number, MainActivity.this);

答案 1 :(得分:0)

您可以使用ApplicationClass.getinstance()。getApplicationContext();

答案 2 :(得分:0)

编辑3

在MainActivity中执行此操作:

Object = new myClass(age,number,MainActivity.this);

在myClass中执行此操作

Toast.makeText(context, "Message", Toast.LENGTH_LONG).show();

修改2

 class MyClass extends AsyncTask<Void, Void, Void> {

  int SPLASH_SHOW_TIME=3000;

  Context context;

       public MyClass(Context context ) {
        this.context = context;
    }
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0) {
    try {
        Thread.sleep(SPLASH_SHOW_TIME);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    Toast.makeText(context, "Created a server socket",Toast.LENGTH_LONG).show(); 

}

}