android handler loop

2019-02-21 07:04发布

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)

2条回答
▲ chillily
2楼-- · 2019-02-21 07:39

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

查看更多
smile是对你的礼貌
3楼-- · 2019-02-21 07:40

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) {
            }
    }
}
查看更多
登录 后发表回答