【问题标题】:Showing toasts from a non-activity class. Ploblem setting the context to the toast [duplicate]展示非活动类的祝酒词。将上下文设置为吐司的问题[重复]
【发布时间】:2015-08-28 06:21:43
【问题描述】:

剧透:由于习惯于 C 和 Java 编程新手,这篇文章可能包含一些愚蠢的内容

有一个活动 MainActivity 和一个包含许多方法的公共非活动类。我需要为其中一些显示吐司警报

当前的尝试是这样的,但对于 getApplicationContext() 失败并显示“无法从静态上下文引用非静态方法”:

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

那么,如何从非活动类中调用 toast?

UPD:errorWarn 将从类中的方法调用。所以,如果类的某个方法发生错误,应该会有一个警告

我们在 MainActivity 中有一个 editText 字段。该类应该从中获取并解析字符串。如果在某些步骤处理失败,它会在 MainActivity 中显示一个 toast

UPD2:完整的结构。

主活动:

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

类:

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

【问题讨论】:

  • 如果你必须:static void errorWarn (Context context, String warning) { ... } 当你需要展示吐司时,你总是可以有一个适当的上下文

标签: java android android-toast


【解决方案1】:

定义您的方法,使其接受 Context 作为参数并将您的 Activity 传递给它。

在你的其他类中

public class YourOtherClass {

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

或者,如果您想在构造函数中使用 Context 并仅在 YourOtherClass 中使用 showToast

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

在 MainActivity 中

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

或者如果你有一个YourOtherClassContext 成员变量并且你想在YourOtherClass 的构造函数中传递Context,你会这样做

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.

对于您在提供的代码中遇到的错误:

Context context = com.example.ex3.MainActivity;

这会失败,因为您试图将类型分配给实例。

MainActivity.errorWarn("here");

这会失败,因为您正在调用非静态方法(该方法的签名中没有静态修饰符),就好像它是静态方法一样。请查看this question,了解有关静态与非静态方法的更多详细信息。

在不知道 YourOtherClass 做什么或它的生命周期如何与 Activity 相关联的情况下,很难说,但必须从与 UI 无关且没有任何引用 Context 的类中触摸 UI可以用,感觉怪怪的。将Context 作为YourOtherClass 的构造函数的参数可能是您需要的,但要小心泄漏ContextActivity 生命周期。

【讨论】:

  • 问题是要从类中调用 showToast,但我收到类似“无法从静态上下文引用非静态 x”的消息
  • 有没有办法将类声明为非静态,以便从中调用非静态方法没有问题?看起来它会解决一切
  • 我编辑了我的答案,希望它更接近你想要的。不过,我鼓励您反思最后一段:也许有一个简单的出路(在构造函数中传递 Context 并在 showToast 中使用它)?更笼统地说,您遇到的这些错误与 Java 相关,而不是特定于 Android,因此我建议您阅读有关纯 Java 的内容(特别是为您准备 Java SE 认证的书籍,这些认证重点关注语言技术,以及 Effective Java,其中在资源有限的情况下必须阅读),它将使您成为更好的 Android 程序员;)
  • 我想要实现的东西是在 UPD2 中。最后,我意识到 Java 类比 C 中的 .c-.h 拆分要聪明得多。例如,创建一个类的实例以使用非静态方法而不是仅仅使用它们的必要性目前看起来很奇怪。还有JNI之类的东西,但看起来不是很好的解决方案。是时候更深入地学习一些基础知识了:)
【解决方案2】:

传递上下文参数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多