gpt4 book ai didi

java - Android:无法在未调用 Looper.prepare() 的线程内创建处理程序

转载 作者:行者123 更新时间:2023-12-01 05:40:33 24 4
gpt4 key购买 nike

我尝试运行以下命令,但收到错误

Can't create handler inside thread that has not called 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());
}

}

如果我将 Looper.prepare() 放在 requestLocationUpdates() 之前,错误就会消失,但不会触发任何事件。

如果我将 Looper.loop() 放在 requestLocationUpdates() 之后,它将暂停并且不会调用“完成”日志。

如果在 runOnUiThread 中调用该函数,如下所示:

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

没有事件被触发。

我将其作为 Unity 游戏引擎的插件运行,因此我无法直接访问主线程。

我尝试在 Untiy 之外运行此代码,并且运行良好。

我对这个问题非常困惑,因此我们将不胜感激。

干杯

<小时/>

更新:

所以我现在尝试检索单独 Activity 中的位置。但是,事件仍未被调用。

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

View 已正确打开,并且 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"/>

最佳答案

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

关于java - Android:无法在未调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7294255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com