Android 处理程序循环

发布于 2024-11-16 21:05:56 字数 1018 浏览 1 评论 0原文

谁能告诉我这个 logcat 有什么问题吗

06-23 11:09:12.060: ERROR/AndroidRuntime(1116): FATAL EXCEPTION: Speedometer
06-23 11:09:12.060: ERROR/AndroidRuntime(1116): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at android.os.Handler.<init>(Handler.java:121)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at android.widget.Toast.<init>(Toast.java:68)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at android.widget.Toast.makeText(Toast.java:231)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at com.paad.whereami.WhereAmI.updateGUI(WhereAmI.java:883)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at com.paad.whereami.WhereAmI.access$5(WhereAmI.java:860)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at com.paad.whereami.WhereAmI$4.run(WhereAmI.java:845)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at java.util.Timer$TimerImpl.run(Timer.java:284)

can any one please tell me what is the problem with this logcat please

06-23 11:09:12.060: ERROR/AndroidRuntime(1116): FATAL EXCEPTION: Speedometer
06-23 11:09:12.060: ERROR/AndroidRuntime(1116): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at android.os.Handler.<init>(Handler.java:121)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at android.widget.Toast.<init>(Toast.java:68)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at android.widget.Toast.makeText(Toast.java:231)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at com.paad.whereami.WhereAmI.updateGUI(WhereAmI.java:883)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at com.paad.whereami.WhereAmI.access$5(WhereAmI.java:860)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at com.paad.whereami.WhereAmI$4.run(WhereAmI.java:845)
06-23 11:09:12.060: ERROR/AndroidRuntime(1116):     at java.util.Timer$TimerImpl.run(Timer.java:284)

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

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

发布评论

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

评论(2

疯狂的代价 2024-11-23 21:05:56

您尝试在您创建的新线程中启动 Android 事件处理程序,例如 onLocationChanged。在线程类的“run()”方法中,需要调用Looper.prepare(),注册事件处理程序,然后调用Looper.loop()。当线程完成后,调用 Looper.myLooper().quit() 方法退出。

例子:

public class MyActivity extends Activity {
    private Thread thread = new ThreadClass();
    private static Looper threadLooper = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // Begin the location reading thread.
            thread.start();

            // do UI stuff in here
            // never sleep in UI thread.  Example only.
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // end the thread.
            threadLooper.quit();
            // quit the activity
            this.finish();
    }

    private class ThreadClass extends Thread {
        @Override
        public void run() {
            Looper.prepare();

            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            MyLocationListener locListen = new MyLocationListener();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, locListen);

            threadLooper = Looper.myLooper();

            Looper.loop();  // loop until "quit()" is called.

            // remove the update listener to prevent the locationManager from calling it.
            locationManager.removeUpdates(locListen);
        }
    }

    private class MyLocationListener implements LocationListener {      
        @Override
        public void onLocationChanged(Location location) {
            /* Do very intensive work here */
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            }
    }
}

You're trying to start an Android event handler such as onLocationChanged inside a new thread that you created. In the "run()" method of the thread class, you need to call Looper.prepare(), register the event handler, then call Looper.loop(). When you're done with the thread, call the Looper.myLooper().quit() method to exit.

Example:

public class MyActivity extends Activity {
    private Thread thread = new ThreadClass();
    private static Looper threadLooper = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            // Begin the location reading thread.
            thread.start();

            // do UI stuff in here
            // never sleep in UI thread.  Example only.
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // end the thread.
            threadLooper.quit();
            // quit the activity
            this.finish();
    }

    private class ThreadClass extends Thread {
        @Override
        public void run() {
            Looper.prepare();

            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            MyLocationListener locListen = new MyLocationListener();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, locListen);

            threadLooper = Looper.myLooper();

            Looper.loop();  // loop until "quit()" is called.

            // remove the update listener to prevent the locationManager from calling it.
            locationManager.removeUpdates(locListen);
        }
    }

    private class MyLocationListener implements LocationListener {      
        @Override
        public void onLocationChanged(Location location) {
            /* Do very intensive work here */
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            }
    }
}
话少情深 2024-11-23 21:05:56

Yoi 需要从 ui 线程显示 toast。您可以使用 Activity 上的 runOnUiThread 方法来完成此操作。
以下是示例的解释 Android:线程中的 Toast

Yoi will need to show toast from ui thread. You can do it using runOnUiThread method on Activity.
Here is an explanation with example Android: Toast in a thread

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