Search code examples
androidtoastandroid-toast

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


I want to show a toast message but getContext() in Toast.makeText((getContext()," Message" , Toat.LENGTH_LONG.show())) is giving error

Cannot resolve Method.

The problem is that in which class I want to show the Toast message is not MainActivity class. This is AsyncTask class. Can i show Toast message in other classes (other than MainActivity class) as the above mentioned problem?

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

    }
}

Edit I made the constructor (See above code) but in the MainActivity class I am calling in this way myClassObj = new myClass(a, b,this); but is giving error

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)

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


}

SecondClass.

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

Solution

  • When you are using this it refers to the enclosing class. In your case this is View.OnClickListener. But you need to pass the context of your Activity.

    So you need to call it this way,

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