getContext ()、getApplicationContext()、getBaseContext 在 Android 中不起作用

getContext () , getApplicationContext(), getBaseContext are not working in Android

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

Cannot resolve Method.

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

进口android.os.AsyncTask; 导入 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 class 中我以这种方式调用 myClassObj = new myClass(a, b,this); 但给出了错误

myClas() in myClass cannot be applied to: Expected Actual Parameters Arguments a: double a b: double b context: android.content.Context this(anonymous...view.View.OnClickListener)

编辑3

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();
        }
    }

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

编辑 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(); 

}

}

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

的上下文

所以你需要这样称呼它,

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