Android toast.makeText 上下文错误

发布于 2024-11-04 18:17:07 字数 1166 浏览 1 评论 0原文

我在位置侦听器内调用 toast.Maketext 时遇到问题。上下文不可用,我做错了什么?

private LocationListener ll = new LocationListener() {

    public void onLocationChanged(Location l) {
        // SMSReceiver.l = l;
        String s = "";
        s += "\tTime: " + l.getTime() + "\n";
        s += "\tLatitude:  " + l.getLatitude() + "°\n";
        s += "\tLongitude: " + l.getLongitude() + "°\n";
        s += "\tAccuracy:  " + l.getAccuracy() + " metres\n";
        s += "\tAltitude:  " + l.getAltitude() + " metres\n";
        s += "\tSpeed:  " + l.getSpeed() + " metres\n";

        // TODO Auto-generated method stub
        if (l.hasSpeed()) {
            mySpeed = l.getSpeed();
        }

        Log.i(DEBUG_TAG, "On Location Changed: (" + s + ")");
ERROR HERE-->       Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }

    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

};

I am having trouble calling toast.Maketext inside of a location listener. The context is not available, what am I doing wrong?

private LocationListener ll = new LocationListener() {

    public void onLocationChanged(Location l) {
        // SMSReceiver.l = l;
        String s = "";
        s += "\tTime: " + l.getTime() + "\n";
        s += "\tLatitude:  " + l.getLatitude() + "°\n";
        s += "\tLongitude: " + l.getLongitude() + "°\n";
        s += "\tAccuracy:  " + l.getAccuracy() + " metres\n";
        s += "\tAltitude:  " + l.getAltitude() + " metres\n";
        s += "\tSpeed:  " + l.getSpeed() + " metres\n";

        // TODO Auto-generated method stub
        if (l.hasSpeed()) {
            mySpeed = l.getSpeed();
        }

        Log.i(DEBUG_TAG, "On Location Changed: (" + s + ")");
ERROR HERE-->       Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }

    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-11-11 18:17:07

声明位于活动类内部(例如:MyActivity),则应将 Toast 创建为:

Toast.makeText(MyActivity.this, s, Toast.LENGTH_SHORT).show();

如果此 LocationListener 在无上下文类中声明,就像您的例子中的 BroadcastReceiver 一样,您可以将上下文传递给其构造函数:

private final class MyReceiver extends BroadcastReceiver
{
    private MyLocationListener listener; 
    public MyReceiver(final Context context)
    {
        this.listener = new MyLocationListener(context);
    }

    private final class MyLocationListener implements LocationListener
    {
        private Context context;
        public MyLocationListener(final Context context)
        {
            this.context = context; 
        }

        @Override
        public void onLocationChanged(Location location)
        {
            // ...
            Toast.makeText(context, "Toast message here", Toast.LENGTH_SHORT).show();
        }

        // implement the rest of the methods
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // Note that you have a context here, which you can use when receiving an broadcast message
    }
}

If this LocationListener declaration is inside an activity class (say: MyActivity), you should create the Toast as:

Toast.makeText(MyActivity.this, s, Toast.LENGTH_SHORT).show();

In case the LocationListener is declared in a contextless class, like in your case a BroadcastReceiver, you can pass the context to its constructor:

private final class MyReceiver extends BroadcastReceiver
{
    private MyLocationListener listener; 
    public MyReceiver(final Context context)
    {
        this.listener = new MyLocationListener(context);
    }

    private final class MyLocationListener implements LocationListener
    {
        private Context context;
        public MyLocationListener(final Context context)
        {
            this.context = context; 
        }

        @Override
        public void onLocationChanged(Location location)
        {
            // ...
            Toast.makeText(context, "Toast message here", Toast.LENGTH_SHORT).show();
        }

        // implement the rest of the methods
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // Note that you have a context here, which you can use when receiving an broadcast message
    }
}
皓月长歌 2024-11-11 18:17:07

确保使用 Activity 类的上下文。如果您在 Activity 中使用此 toast,请编写 Classname.this 代替 context

Make sure that you use the context of the Activity class.If you are using this toast in an Activity, write, Classname.this in place of context

谁的新欢旧爱 2024-11-11 18:17:07

由于上下文不可用,您可以将其传递到构造函数中

Since context is not available,you can pass it in constructor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文