安卓处理循环 [英] android handler loop

查看:131
本文介绍了安卓处理循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个可以请告诉我什么是这个logcat的问题,请

  11月6日至23号:09:12.060:ERROR / AndroidRuntime(1116):致命异常:车速表
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):java.lang.RuntimeException的:内螺纹已经不叫尺蠖prepare无法创建处理器()
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在android.os.Handler< INIT>(Handler.java:121)
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在android.widget.Toast< INIT>(Toast.java:68)
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在android.widget.Toast.makeText(Toast.java:231)
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在com.paad.whereami.WhereAmI.updateGUI(WhereAmI.java:883)
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在com.paad.whereami.WhereAmI.access $ 5(WhereAmI.java:860)
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在com.paad.whereami.WhereAmI $ 4.run(WhereAmI.java:845)
11月6号至23号:09:12.060:ERROR / AndroidRuntime(1116):在java.util.Timer中的$ TimerImpl.run(Timer.java:284)
 

解决方案

您正试图启动一个Android的事件处理程序,如onLocationChanged里面你创建一个新的线程。在运行()Thread类的方法,你需要调用活套。prepare(),注册事件处理程序,然后调用Looper.loop()。当你的线程执行,调用Looper.myLooper()。退出()方法来退出。

例如:

 公共类MyActivity延伸活动{
    私人线程的线程=新ThreadClass();
    私有静态活套threadLooper = NULL;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.main);

            //开始的位置读取线程。
            thread.start();

            //做UI的东西在这里
            //从不睡在UI线程。举例而已。
            尝试 {
                视频下载(4000);
            }赶上(InterruptedException异常E){
                e.printStackTrace();
            }

            //结束线程。
            threadLooper.quit();
            //退出活动
            this.finish();
    }

    私有类ThreadClass继承Thread {
        @覆盖
        公共无效的run(){
            活套prepare()。

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

            threadLooper = Looper.myLooper();

            Looper.loop(); //循环直到退出()之称。

            //从调用它删除更新监听器prevent的locationManager。
            locationManager.removeUpdates(locListen);
        }
    }

    私有类MyLocationListener实现LocationListener的{
        @覆盖
        公共无效onLocationChanged(位置定位){
            / *做的非常密集的工作在这里* /
        }

        @覆盖
        公共无效onProviderDisabled(字符串提供商){
        }

        @覆盖
        公共无效onProviderEnabled(字符串提供商){
        }

        @覆盖
        公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
            }
    }
}
 

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)

解决方案

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) {
            }
    }
}

这篇关于安卓处理循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆