Showing toasts from a non-activity class. Ploblem setting the context to the toast [duplicate]
Asked Answered
S

2

5

Spoiler: this post may contain something stupid due to being used to C and new to Java programming

There is an activity MainActivity and a public non-activity class containing a number of methods. I need to show toast alerts for some of them

Current attempts are something like this, which fails with "Non-static method can not be referenced from a static context" for getApplicationContext():

void errorWarn (String warning) {
    Context context = android.content.ContextWrapper.getApplicationContext();
    Toast.makeText(context, "Something's wrong in " + warning, Toast.LENGTH_SHORT).show();
}

So, how to call toasts from a non-activity class?

UPD: The errorWarn is to be called from the methods in the class. So, if an error in a method of the class occurs, there should be an alert

We are in the MainActivity having an editText field. The class should get and parse the string from it. If on some step processing fails, it shows a toast in the MainActivity

UPD2: The full structure.

MainActivity:

public class MainActivity extends ActionBarActivity {
    <...>
    public void ButtonClick (View view) {
        Class.testfunc("");
    }
}

Class:

public class Class {
    void errorWarn (Context context, String warning) {
        Toast.makeText(context, "Something must be wrong. " + warning, Toast.LENGTH_SHORT).show();
    }
    void testfunc (String string) {
        errorWarn(string);
    }
}
Sherman answered 12/6, 2015 at 9:28 Comment(1)
if you have to: static void errorWarn (Context context, String warning) { ... } as when you need to show the toast you always can have a proper contextSibel
K
8

Define your method so that it takes a Context in argument and pass your Activity to it.

in YourOtherClass

public class YourOtherClass {

    public void showToast(Context context, String message){
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
}

or if you want to take a Context in your constructor and use showToast only in YourOtherClass :

public class YourOtherClass {

    private Context context;

    public YourOtherClass(Context context){
        this.context = context;
    }

    private void showToast(String message){
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
}

in MainActivity

new YourOtherClass().showToast(this, message);

or if you have a Context member variable of YourOtherClass and you want to pass the Context in the constructor of YourOtherClass, you would do

new YourOtherClass(this).showToast(message);
// showToast doesn't have to take a Context as argument, it could just take one as constructor parameter and hold that. 
// But then you have to make sure YourOtherClass is not used anymore if the Activity is closed.

And for the errors you got in the code your provided :

Context context = com.example.ex3.MainActivity;

This fails because you are trying to assign a type to an instance.

MainActivity.errorWarn("here");

This fails because you are calling a non-static method (the method has no static modifier in its signature) as if it was a static one. Have a look at this question for more details on static vs non-static methods.

Without knowing what YourOtherClass does or how its lifecycle is linked to the Activity's, it's hard to say, but having to touch UI from a class that is not UI related and doesn't have any reference to Context your can use, that feels odd. Taking a Context as argument of the constructor of YourOtherClass is probably what you need, but be wary of leaking Context and Activity lifecycle.

Kamilah answered 12/6, 2015 at 9:34 Comment(4)
The problem is showToast is to be called from the class, but I get messages like "Non-static x can not be referenced from a static context"Sherman
Is there a way to declare the class as non-static so that there is no problem to call non-static methods from it? Looks like it would solve everythingSherman
I edited my answer, hopefully it's closer to what you want. Nevertheless, I encourage you to reflect on the last paragraph : maybe there's an easy way out (passing Context in the constructor and using it in showToast) ? On a more general note, those errors you got were more Java related than Android specific, so I'd recommend you read about pure Java (particularly books preparing you for Java SE certifications that have a heavy focus on language technicalities, and Effective Java which is a must-read when working with limited resources), it will make you a better Android programmer ;)Kamilah
The thing I wanted to implement is in the UPD2. At last I realized that Java classes are way smarter than .c-.h splitting in C. For example, a necessity to create an instance of a class to use methods non-static instead of just using them seems weird at the moment. There is also a thing like JNI, but it does not look like a good solution. Time to learn some basics more deeply :)Sherman
C
1

Pass context param

 void errorWarn (Context context,String warning) {
        Toast.makeText(context, "Something's in wrong " + warning, Toast.LENGTH_SHORT).show();
    }
Currajong answered 12/6, 2015 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.