安卓 Toast.makeText 第一个参数的问题,总是出错

Toast.makeText 第一个参数的问题,总是出错

尝试了
getApplicationContext()
this
hots.this

就是一个普通的类 ! 好郁闷啊

public class hots extends UZtop {

    public void nowsa(){
        String showMsg = moduleContext.optString("msg");
        Log.d("name", "DDD:收到消息"+showMsg);

        Toast.makeText(getApplicationContext(), "出错了", Toast.LENGTH_LONG).show();

    }

}

1 调用nowsa()的时候传入context,
public void nowsa(Context context){
String showMsg = moduleContext.optString("msg");
Log.d("name", "DDD:收到消息"+showMsg);
Toast.makeText(context, "出错了", Toast.LENGTH_LONG).show();
}

2 在自己的Application中创建全局的Context
public class MyApplication extends Application {
private static Context context;

@Override
public void onCreate() {
    super.onCreate();
    context = this;
}
    .......
public static Context getAppContext() {
    return context;
}

}
然后 public void nowsa(){
String showMsg = moduleContext.optString("msg");
Log.d("name", "DDD:收到消息"+showMsg);

    Toast.makeText(MyApplication.getAppContext(), "出错了", Toast.LENGTH_LONG).show();

}

https://zhidao.baidu.com/question/541644431.html

第一個參數必須要是Activity or Service, 嘗試傳activity 進來當參數

getContext()就可以了, getApplicationContext 获取的上Application对象