我添加了一个加载器,因为我的列表类要求更新位置。但是,在DDMS中设置位置后,进程将开始关闭,直到进程列表为空,并且屏幕变黑。抛出的唯一错误消息如下。崩溃肯定是由onLocationChanged被调用引起的,但我不知道这是怎么发生的。
E/InputQueue-JNI(335): channel '406d08f8 com.example.test/com.example.test.SplashActivity (client)' ~ Publisher closed input channel or an error occurred. events=0x8
MyContent.Latitude和Longitude是静态Floats,意图在添加位置服务之前就已开始工作。
SplashActivity.java
package com.example.test;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
public class SplashActivity extends Activity implements LocationListener {
boolean haveLocation;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
haveLocation = false;
MyLoader lb = new MyLoader();
lb.execute(this);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location currLoc = lm.getLastKnownLocation(lm.getBestProvider(
new Criteria(), false));
if (currLoc == null)
lm.requestSingleUpdate(new Criteria(), this, null);
else
onLocationChanged(currLoc);
}
public class MyLoader extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... args)
{
while (!haveLocation)
{
try { wait(500); } catch (Exception e) {}
}
return null;
}
protected void onPostExecute(Void arg)
{
Intent intent = new Intent(SplashActivity.this,
ListActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
@Override
public void onLocationChanged(Location location)
{
MyContent.LATITUDE = (float) location.getLatitude();
MyContent.LONGITUDE = (float) location.getLongitude();
haveLocation = true;
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.ListActivity"
android:label="@string/app_name" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SplashActivity">
</activity>
</application>
</manifest>
谢谢。
答案 0 :(得分:0)
LocationManager lm = getSystemService(Context.LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
lm.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
else if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
lm.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
else {
try {
throw (new Exception("No location provider is available!"));
} catch (Exception e) {
e.printStackTrace();
}
}
可能默认的“最佳”位置提供商导致了问题。现在我使用了两个常量提供者,这就像一个魅力。如果有人对原因有解释,那就太好了,但对于遇到这个问题的其他人来说,这是一个解决方案。