0

我添加了一个加载器,因为我的列表类需要更新位置。但是,在 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 是静态浮点数,意图在添加位置服务之前开始工作。

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>

谢谢你。

4

1 回答 1

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

可能是默认的“最佳”位置提供程序导致了问题。现在我使用了两个常量提供者,这就像一个魅力。如果有人对原因有解释,那就太好了,但这对于遇到此问题的其他人来说是一个解决方案。

于 2013-02-07T18:00:31.923 回答