【问题标题】:getting exception in runOnFirstFix在 runOnFirstFix 中出现异常
【发布时间】:2012-09-19 17:15:00
【问题描述】:

我收到以下异常

09-19 18:09:36.551: E/AndroidRuntime(20608): FATAL EXCEPTION: Thread-2852
09-19 18:09:36.551: E/AndroidRuntime(20608): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-19 18:09:36.551: E/AndroidRuntime(20608):    at android.os.Handler.<init>(Handler.java:121)
09-19 18:09:36.551: E/AndroidRuntime(20608):    at android.widget.Toast$TN.<init>(Toast.java:317)
09-19 18:09:36.551: E/AndroidRuntime(20608):    at android.widget.Toast.<init>(Toast.java:91)
09-19 18:09:36.551: E/AndroidRuntime(20608):    at android.widget.Toast.makeText(Toast.java:233)
09-19 18:09:36.551: E/AndroidRuntime(20608):    at com.mid.nash.activity.MapPageFragment$3$1.run(MapPageFragment.java:156)
09-19 18:09:36.551: E/AndroidRuntime(20608):    at java.lang.Thread.run(Thread.java:856)

以下是我的代码

myLocationOverlay.runOnFirstFix(new Runnable() {
    public void run() {
        //check if the location is within boundaries. 
        GeoPoint currentlocation = myLocationOverlay.getMyLocation();
        double latitude = (currentlocation.getLatitudeE6() / 1E6);
        double longitude = (currentlocation.getLongitudeE6() / 1E6);
        //check if user is witin boundry of Kew
        if (latitude <= RESTRICTED_UPPERLEFT_LAT && 
                latitude >= RESTRICTED_BOTTOMRIGHT_LAT && 
                longitude >= RESTRICTED_UPPERLEFT_LNG && 
                longitude <= RESTRICTED_BOTTOMRIGHT_LNG){
            mapController.animateTo(myLocationOverlay.getMyLocation());
        } else {
            myLocationOverlay.disableMyLocation();
            Toast.makeText(getActivity(), "You are out of scope.", Toast.LENGTH_LONG);
        }
    }
});

我已经提到this link 认为这可能对我有帮助。但我无法实现上述方法。

如何摆脱这个异常?

【问题讨论】:

    标签: android multithreading toast runnable


    【解决方案1】:

    问题是Runnable 在没有Looper 的线程上执行,而Looper 是发布Toast 所必需的。您需要做的是在您的MapPageFragment 中创建一个Handler,您可以向它发送消息以显示Toast。或者根本不使用Toast,您可以实现更好的通知UI。

    【讨论】:

      【解决方案2】:

      我建议将 Toast 从 Runnable 中移出。

      应该在主线程中与其他 UI 元素一起调用 Toast。昨天我遇到了一个问题,后台线程被破坏并且 Toast 从未关闭。它出现在主屏幕和其他任何地方,直到我强行删除应用程序的缓存内存...... Toast 很好,但将它们保留在主线程上。

      请注意,您不要调用 show(),因此 Toast 目前不会做任何事情......

      【讨论】:

        猜你喜欢
        • 2019-04-09
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 2021-09-30
        • 1970-01-01
        • 2011-10-28
        • 1970-01-01
        相关资源
        最近更新 更多