2013-07-04 65 views
1

我在下面的代碼中得到了着名的「無法在未調用Looper.prepare()的線程中創建處理程序」錯誤。基於我的大部分谷歌搜索結果,當我嘗試從我的非用戶線程更新用戶界面時,會發生這種情況,但是當我從其他助手類調用該方法時,此時不更新用戶界面(僅在地址已經回);所以請諮詢爲什麼發生這種情況將不勝感激。錯誤:無法在未調用Looper.prepare()的線程中創建處理程序

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(); 
    } 
} 

這是我得到的例外:

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) 

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

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

這是我ConnectionHandler Helper類:

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

回答

2

好的問題是requestLocationUpdates是一個異步調用,這意味着你不需要另一個線程上運行它也意味着,如果你嘗試,你正在有效地使另一個線程上的處理程序是不允許的。因此,你可以刪除線程,只是運行這些上的onCreate()(因爲你只是做異步調用)

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

相反,你可以把你的電話syncronous在另一個線程...

@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(); 
} 
+0

也嘗試了你的代碼,並且仍然得到相同的Looper.prepare()異常。我的代碼在ConnectionHandler helper類中引用了這一行:mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,50,mlocationListener); – kevoroid

+0

你可以添加ConnectionHandler的代碼嗎? –

+0

當然。添加到原始帖子。 – kevoroid

1

您無法更新TextView從非UI線程。

使用runOnUiThread代替:

runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         dt2.setText(textPageConn.mAddress); 
        } 
       }); 
+0

我做到了。它沒有工作。在「do」之後,我寫道:runOnUiThread(new Runnable(){...然後運行()覆蓋...... – kevoroid

+1

我建議你註釋掉'dt2.setText()'行,然後寫'Log.v(「MY_APP 「,」here「)'如果你沒有得到'Looper.prepare()'異常並且看到日誌中的」here「,那麼問題出現在'setText()'中。 – azizbekian

+0

我確實評論過仍然有Looper.prepare()異常! – kevoroid

0

它不可能改變在其他線程的UI內容..你可以在基本方法

  • 解決這些問題,使用[R unOnUiThread()方法,其操作簡單,但它不是最好的做法
  • 另一種方法是使用處理器與回調處理器(回撥三),其推薦的一個

我會建議使用的處理程序用回調方法。 你可以在這裏獲得很好的教程。 handler with callback in thread

相關問題