2011-09-03 48 views
1

我試圖運行下面的,但我得到的錯誤Android的:不能內螺紋已不叫Looper.prepare創建處理程序()

無法創建內螺紋處理程序尚未調用Looper.prepare()

這裏是我的代碼

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

} 

如果我的requestLocationUpdates()錯誤消失,但沒有EV之前把Looper.prepare() ents被解僱。

如果我然後在requestLocationUpdates()之後放置Looper.loop()它會暫停並且不會調用「完成」日誌。

如果如果內runOnUiThread調用函數像這樣:

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

沒有事件被觸發。

我運行這個作爲Unity遊戲引擎的插件,所以我沒有直接訪問主線程。

我試過在Untiy之外運行這段代碼,它運行良好。

我很堅持這一個,所以任何幫助將不勝感激。

乾杯


更新:

所以我現在正在努力獲取一個單獨的活動裏面的位置。但是,這些事件仍未被調用。

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

該視圖可以正確打開,並且GPS和網絡都可用。仍然沒有燒事件

進出口使用這些權限

<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"/> 
+1

你們有任何看法? – Ronnie

+0

也從getLocations方法中移除靜態。 – Ronnie

+0

我現在增加了一個視圖,仍然沒有運氣。查看更新。多謝你們。 – user346443

回答

0

嘗試mView.post(new Runnable() { getLocations() };

+0

給了它一個鏡頭仍然沒有運氣 – user346443

相關問題