Android的:不能内螺纹已不叫尺蠖prepare创造处理器() [英] Android: Can't create handler inside thread that has not called Looper.prepare()

查看:167
本文介绍了Android的:不能内螺纹已不叫尺蠖prepare创造处理器()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行下面的,但我得到的错误

无法创建内螺纹的处理程序已经不叫活套。prepare(

下面是我的code

 私有静态无效的getLocations(){            尝试{            m_locationManager =(的LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
            m_locationListener =新LocationListener的(){
                公共无效onLocationChanged(地点){Log.d(输出,onLocationChanged);}
                公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){Log.d(输出,onStatusChanged);}
                公共无效onProviderEnabled(字符串提供商){Log.d(输出,onProviderEnabled);}
                公共无效onProviderDisabled(字符串提供商){Log.d(输出,onProviderDisabled);}
              };
            m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,1000,m_locationListener);            Log.d(输出,完成);            }赶上(例外前){
                Log.d(输出,ex.getMessage());
            }        }

如果我把活套。prepare()的requestLocationUpdates()之前的错误消失,但没有事件被解雇

如果我再放入Looper.loop()在该requestLocationUpdates()之后,它会暂停和完成日志不会被调用。

如果调用是否在runOnUiThread的功能,像这样:

  activity.runOnUiThread(新的Runnable(){
    公共无效的run(){的getLocations(); }
});

没有事件被解雇了。

进出口运行以此为Unity游戏引擎的插件,所以我不必在主线程直接访问。

我曾尝试运行此code Untiy之外,它运行正常。

林pretty停留在这一个,所以任何帮助将是AP preciated。

干杯


更新:

所以我现在正试图获取一个单独的活动里面的位置。然而,事件还没有被调用。

 公共类LocationActivity扩展活动实现LocationListener的{    私人的LocationManager m_locationManager;    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        内容的LinearLayout =新的LinearLayout(本);
        content.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        content.setOrientation(LinearLayout.VERTICAL);
        content.setBackgroundColor(Color.TRANSPARENT);        TextView的infoLabel =新的TextView(本);
        infoLabel.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        infoLabel.setTextSize(20.0f);
        infoLabel.setText(初始化...);
        content.addView(infoLabel);        尝试{
            m_locationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);            字符串提供商= NULL;            如果(m_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                供应商= LocationManager.GPS_PROVIDER;
                Log.d(团结,使用GPS);
                m_locationManager.requestLocationUpdates(供应商,1000,100,这一点);
            }否则如果(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                供应商= LocationManager.NETWORK_PROVIDER;
                Log.d(团结,使用Netword);
                m_locationManager.requestLocationUpdates(供应商,1000,100,这一点);
            }其他{
                Log.d(团结,提供者不可用);
            }        }赶上(例外前){
            Log.d(团结,locatons错误+ ex.getMessage());
        }        的setContentView(内容);    }    公共无效onLocationChanged(地点){
        Log.d(团结,UserLocation纬度:+ location.getLatitude()+长:+ location.getLongitude());
    }
    公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){Log.d(团结,onStatusChanged);}
    公共无效onProviderEnabled(字符串提供商){Log.d(团结,onProviderEnabled);}
    公共无效onProviderDisabled(字符串提供商){Log.d(团结,onProviderDisabled);}
}

该视图被正确打开,以及GPS和网络可用。仍然没有触发事件

使用

林这些权限

 <使用许可权的android:NAME =android.permission.ACCESS_WIFI_STATE/>
    <使用许可权的android:NAME =android.permission.ACCESS_COARSE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_FINE_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_MOCK_LOCATION/>
    <使用许可权的android:NAME =android.permission.ACCESS_GPS/>
    <使用许可权的android:NAME =android.permission.READ_PHONE_STATE/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>


解决方案

尝试 mView.post(新的Runnable(){的getLocations()};

I am trying to run the following but am getting the error

Can't create handler inside thread that has not called Looper.prepare()

Here is my code

private static void getLocations(){

            try{

            m_locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);


            m_locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {Log.d("Output", "onLocationChanged");}
                public void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Output", "onStatusChanged");}
                public void onProviderEnabled(String provider) {Log.d("Output", "onProviderEnabled");}
                public void onProviderDisabled(String provider) {Log.d("Output", "onProviderDisabled");}
              };


            m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1000, m_locationListener);

            Log.d("Output", "done");

            }catch(Exception ex){
                Log.d("Output", ex.getMessage());
            }

        }

If i put Looper.prepare() before the requestLocationUpdates() the error goes away but no events are fired

If i then put Looper.loop() after the the requestLocationUpdates() it pauses and the "done" log is not called.

If if call the function within runOnUiThread like so:

activity.runOnUiThread(new Runnable() {
    public void run() {getLocations(); }
});

no events are fired.

Im running this as a plugin for the Unity game engine so i don't have direct access to the main thread.

I have tried running this code outside of Untiy and it runs fine.

Im pretty stuck on this one so any help would be appreciated.

Cheers


Update:

So i'm now trying to retrieve the locations inside a separate Activity. However, the events are still not being called.

public class LocationActivity extends Activity implements LocationListener{

    private LocationManager m_locationManager;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        LinearLayout content = new LinearLayout(this);
        content.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        content.setOrientation(LinearLayout.VERTICAL);
        content.setBackgroundColor(Color.TRANSPARENT);

        TextView infoLabel = new TextView(this);
        infoLabel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        infoLabel.setTextSize(20.0f);
        infoLabel.setText("Initializing...");
        content.addView(infoLabel);

        try{
            m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            String provider = null;

            if ( m_locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
                provider = LocationManager.GPS_PROVIDER ;
                Log.d("Unity", "Using GPS");
                m_locationManager.requestLocationUpdates(provider, 1000, 100, this);
            } else if(m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                provider = LocationManager.NETWORK_PROVIDER;
                Log.d("Unity", "Using Netword");
                m_locationManager.requestLocationUpdates(provider, 1000, 100, this);
            } else {
                Log.d("Unity", "Provider Not available");
            }

        }catch(Exception ex){
            Log.d("Unity", "locatons error " + ex.getMessage());
        }

        setContentView(content);

    }

    public void onLocationChanged(Location location) {
        Log.d("Unity", "UserLocation Lat:" + location.getLatitude() + " Long:" + location.getLongitude());
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {Log.d("Unity", "onStatusChanged");}
    public void onProviderEnabled(String provider) {Log.d("Unity", "onProviderEnabled");}
    public void onProviderDisabled(String provider) {Log.d("Unity", "onProviderDisabled");}
} 

The view is opened correctly and and Both GPS and Network are available. Still no firing events

Im using these permissions

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_GPS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 

解决方案

Try mView.post(new Runnable() { getLocations() };

这篇关于Android的:不能内螺纹已不叫尺蠖prepare创造处理器()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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