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

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

问题描述

即时得到了著名的无法创建线程里面已经没有所谓的活套。prepare()处理在我的下面code错误。根据我的大多数谷歌的搜索结果中,出现这种情况时,我试图从我无的UI线程更新用户界面,但即时通讯在这一点上没有更新的用户界面时,我从我的其他辅助类调用方法(仅在地址已经回);所以请在为什么发生这种情况是AP preciated咨询。

 包com.parspake.kookojapost;进口android.app.ActionBar;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.database.sqlite.SQLiteDatabase;
导入android.location *。
进口android.os.Build;
进口android.os.Bundle;
进口android.os.Handler;
进口android.os.Looper;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;公共类TextShare延伸活动{
    ConnectionHandler textPageConn;
    TextView的DT2;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.text_share);        textPageConn =新ConnectionHandler(本);
        DT2 =(的TextView)findViewById(R.id.show_location_text_page);        线程线程1 =新主题(新的Runnable(){            @覆盖
            公共无效的run(){                textPageConn.getLocation();
                做{                    如果(textPageConn.mAddress!= NULL){
                        dt2.setText(textPageConn.mAddress);
                        打破;
                    }其他{
                        Log.d(调试,没有位置);
                    }                    尝试{
                        视频下载(1000);
                    }赶上(InterruptedException的E){
                        Thread.currentThread()中断()。
                    }
                }而(textPageConn.mAddress == NULL);
            }        });
        thread1.start();
    }
}

这是我得到的异常:

  07-04 23:59:38.730:E / AndroidRuntime(7149):致命异常:螺纹8482
07-04 23:59:38.730:E / AndroidRuntime(7149):了java.lang.RuntimeException:无法内螺纹已不叫尺蠖prepare创造处理器()
07-04 23:59:38.730:E / AndroidRuntime(7149):在android.os.Handler<&初始化GT;(Handler.java:121)
07-04 23:59:38.730:E / AndroidRuntime(7149):在android.location.LocationManager $ ListenerTransport $ 1<&初始化GT;(LocationManager.java:183)
07-04 23:59:38.730:E / AndroidRuntime(7149):在android.location.LocationManager $ ListenerTransport<&初始化GT;(LocationManager.java:183)
07-04 23:59:38.730:E / AndroidRuntime(7149):在android.location.LocationManager._requestLocationUpdates(LocationManager.java:661)
07-04 23:59:38.730:E / AndroidRuntime(7149):在android.location.LocationManager.requestLocationUpdates(LocationManager.java:486)
07-04 23:59:38.730:E / AndroidRuntime(7149):在com.parspake.kookojapost.ConnectionHandler.getLocation(ConnectionHandler.java:136)
07-04 23:59:38.730:E / AndroidRuntime(7149):在com.parspake.kookojapost.TextShare $ 2.run(TextShare.java:69)
07-04 23:59:38.730:E / AndroidRuntime(7149):在java.lang.Thread.run(Thread.java:856)

所以ConnectionHandler.java:136是 - > mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,50,mlocationListener);

和TextShare.java:69是 - > textPageConn.getLocation();

这是我的ConnectionHandler Helper类:

 包com.parspake.kookojapost;进口android.content.Context;
导入android.location *。
进口android.net.ConnectivityManager;
进口android.net.NetworkInfo;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.Toast;进口java.io.IOException异常;
进口的java.util.List;
进口java.util.Locale中;公共类ConnectionHandler {    私人上下文CTX;
    双多点定位;
    双mLong;
    串currCity;
    串currCountry;
    串mAddress;
    的LocationManager mLocationManager;
    LocationListener的mlocationListener;    公共ConnectionHandler(上下文的背景下){
        this.ctx =背景;
    }
    公共布尔locationSourceEnabled(){
        mLocationManager =(的LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
        布尔isInternetLocationAvailable = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        布尔isGpsAvailable = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        如果(isInternetLocationAvailable){
            返回true;
        }否则如果(isGpsAvailable){
            返回true;
        }
        返回false;
    }
    公共无效的getLocation(){        mlocationListener =新LocationListener的(){
            @覆盖
            公共无效onLocationChanged(地点){                MLAT = location.getLatitude();
                mLong = location.getLongitude();                地理codeR GCD =新的地缘codeR(CTX,Locale.getDefault());
                清单<地址>地址;
                尝试{
                    地址= gcd.getFromLocation(MLAT,mLong,1);
                    如果(addresses.size()大于0){
                        currCity = addresses.get(0).getLocality();
                        currCountry = addresses.get(0).getCountryName();
                        mAddress = currCity + - + currCountry;
                    }
                }赶上(IOException异常五){
                    Log.d(OMID调试,e.getMessage());
                }
            }            @覆盖
            公共无效onStatusChanged(字符串s INT I,捆绑软件包){
            }            @覆盖
            公共无效onProviderEnabled(String s)将{
            }            @覆盖
            公共无效onProviderDisabled(String s)将{
            }
        };
        mLocationManager =(的LocationManager)ctx.getSystemService(Context.LOCATION_SERVICE);
        如果(locationSourceEnabled()){
            如果(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,50,mlocationListener);
            }
            如果(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,50,mlocationListener);
            }
        }
    }
}


解决方案

确定的问题是,requestLocationUpdates是asyncronous电话,这意味着你不需要在另一个线程中运行它也意味着,如果你尝试你有效地使在其上是不允许的另一个线程的处理程序。因此,你可以删除该线程,只是对所有运行的onCreate这(因为你只是做异步调用)

引用:<一href=\"http://stackoverflow.com/questions/9033337/requestlocationupdates-gives-error-cant-create-handler-inside-thread-that-has\">requestLocationUpdates给出错误&QUOT;内螺纹已不叫尺蠖prepare无法创建处理程序()

相反,你可以把你的电话syncronous在另一个线程...

  @覆盖
     公共无效onLocationChanged(地点){
         新主题(新的Runnable接口(){            @覆盖
            公共无效的run(){
                 MLAT = location.getLatitude();
                 mLong = location.getLongitude();                 地理codeR GCD =新的地缘codeR(CTX,Locale.getDefault());
                 清单&LT;地址&gt;地址;
                 尝试{
                     地址= gcd.getFromLocation(MLAT,mLong,1);
                     如果(addresses.size()大于0){
                         currCity = addresses.get(0).getLocality();
                         currCountry = addresses.get(0).getCountryName();
                         mAddress = currCity + - + currCountry;
                     }
                 }赶上(IOException异常五){
                     Log.d(OMID调试,e.getMessage());
                 }
            }
        })。开始();     }

Im getting that famous "Can't create handler inside thread that has not called Looper.prepare()" error in my below code. based on most of my Google search result, this happens when i try to update UI from my none-ui-thread, but im not updating the UI at this point when i call the method from my other helper class (only after address has been returned); so please consult on why this is happening would be appreciated.

package com.parspake.kookojapost;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.location.*;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TextShare extends Activity {


    ConnectionHandler textPageConn;
    TextView dt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_share);

        textPageConn = new ConnectionHandler(this);
        dt2 = (TextView) findViewById(R.id.show_location_text_page);

        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {

                textPageConn.getLocation();
                do {

                    if (textPageConn.mAddress != null) {
                        dt2.setText(textPageConn.mAddress);
                        break;
                    } else {
                        Log.d("debug","no location");
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                } while (textPageConn.mAddress == null);
            }

        });
        thread1.start();
    }
}

this is the exception that i get:

07-04 23:59:38.730: E/AndroidRuntime(7149): FATAL EXCEPTION: Thread-8482
07-04 23:59:38.730: E/AndroidRuntime(7149): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-04 23:59:38.730: E/AndroidRuntime(7149):     at android.os.Handler.<init>(Handler.java:121)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:183)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:183)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:661)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:486)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at com.parspake.kookojapost.ConnectionHandler.getLocation(ConnectionHandler.java:136)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at com.parspake.kookojapost.TextShare$2.run(TextShare.java:69)
07-04 23:59:38.730: E/AndroidRuntime(7149):     at java.lang.Thread.run(Thread.java:856)

so ConnectionHandler.java:136 is -> mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 50, mlocationListener);

and TextShare.java:69 is -> textPageConn.getLocation();

this is my ConnectionHandler Helper class:

package com.parspake.kookojapost;

import android.content.Context;
import android.location.*;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class ConnectionHandler {

    private Context ctx;
    double mLat;
    double mLong;
    String currCity;
    String currCountry;
    String mAddress;
    LocationManager mLocationManager;
    LocationListener mlocationListener;

    public ConnectionHandler(Context context) {
        this.ctx = context;
    }


    public boolean locationSourceEnabled() {
        mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        boolean isInternetLocationAvailable = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        boolean isGpsAvailable = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (isInternetLocationAvailable) {
            return true;
        } else if (isGpsAvailable) {
            return true;
        }
        return false;
    }


    public void getLocation() {

        mlocationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {

                mLat = location.getLatitude();
                mLong = location.getLongitude();

                Geocoder gcd = new Geocoder(ctx, Locale.getDefault());
                List<Address> addresses;
                try {
                    addresses = gcd.getFromLocation(mLat, mLong, 1);
                    if (addresses.size() > 0) {
                        currCity = addresses.get(0).getLocality();
                        currCountry =  addresses.get(0).getCountryName();
                        mAddress = currCity + " - " + currCountry;
                    }
                } catch (IOException e) {
                    Log.d("omid debug", e.getMessage());
                }
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };
        mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        if (locationSourceEnabled()) {
            if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 50, mlocationListener);
            }
            if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 50, mlocationListener);
            }
        }
    }
}

解决方案

Ok the problem is that requestLocationUpdates is an asyncronous call, meaning that you don't need to run it on another thread and also means if you try to you are effectively making a handler on another thread which is not allowed. So instead you can remove the thread and just run this all on onCreate (since you are only doing async calls)

reference: requestLocationUpdates gives error "Can't create Handler inside thread that has not called Looper.prepare()

Instead you could put your syncronous calls in another thread...

 @Override
     public void onLocationChanged(Location location) {
         new Thread(new Runnable() {

            @Override
            public void run() {
                 mLat = location.getLatitude();
                 mLong = location.getLongitude();

                 Geocoder gcd = new Geocoder(ctx, Locale.getDefault());
                 List<Address> addresses;
                 try {
                     addresses = gcd.getFromLocation(mLat, mLong, 1);
                     if (addresses.size() > 0) {
                         currCity = addresses.get(0).getLocality();
                         currCountry =  addresses.get(0).getCountryName();
                         mAddress = currCity + " - " + currCountry;
                     }
                 } catch (IOException e) {
                     Log.d("omid debug", e.getMessage());
                 }
            }
        }).start();

     }

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

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