我尝试按照http://developer.android.com/training/location/retrieve-current.html
的指南获取当前位置我的google play服务已启动,但我的locationclient仍无法连接,并且在运行程序时出现这些错误。 (PS:以前我的位置代码正常,但现在不能,奇怪)
你去吧
package com.example.Wifin;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class myMap extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,LocationListener {
GoogleMap mMap;
LatLng Current;
Marker here;
LocationClient mLocationClient;
int resultCode;
Location mCurrentLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(ConnectionResult.SUCCESS == resultCode){
Log.d("Location Updates",
"Google Play services is available.");
}
else{
Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
}
mLocationClient = new LocationClient(this, this, this);
mCurrentLocation = mLocationClient.getLastLocation();
Current = new LatLng(mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
here = mMap.addMarker(new MarkerOptions()
.position(Current)
.title("you're here!")
.snippet("Connect to home2, Signal Strenght -20dBm")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.testmarker)));
}
@Override
public void onConnectionFailed(ConnectionResult result) {
}
@Override
public void onConnected(Bundle connectionHint) {
mLocationClient.connect();
}
@Override
public void onDisconnected() {
}
@Override
public void onLocationChanged(Location arg0) {
}
}
下面是我的catlog
09-16 21:31:37.187: D/dalvikvm(14050): GC_FOR_ALLOC freed 259K, 5% free 8612K/8992K, paused 14ms, total 15ms
09-16 21:31:37.447: D/dalvikvm(14050): GC_FOR_ALLOC freed 816K, 10% free 8307K/9176K, paused 15ms, total 15ms
09-16 21:31:37.527: D/dalvikvm(14050): GC_FOR_ALLOC freed 301K, 8% free 8517K/9176K, paused 17ms, total 17ms
09-16 21:31:37.607: D/dalvikvm(14050): GC_FOR_ALLOC freed 311K, 5% free 8717K/9176K, paused 17ms, total 17ms
09-16 21:31:37.627: D/Location Updates(14050): Google Play services is available.
09-16 21:31:37.637: D/AndroidRuntime(14050): Shutting down VM
09-16 21:31:37.637: W/dalvikvm(14050): threadid=1: thread exiting with uncaught exception (group=0x4180f700)
09-16 21:31:37.637: E/AndroidRuntime(14050): FATAL EXCEPTION: main
09-16 21:31:37.637: E/AndroidRuntime(14050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Wifin/com.example.Wifin.myMap}: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.os.Handler.dispatchMessage(Handler.java:99)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.os.Looper.loop(Looper.java:137)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-16 21:31:37.637: E/AndroidRuntime(14050): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 21:31:37.637: E/AndroidRuntime(14050): at java.lang.reflect.Method.invoke(Method.java:525)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-16 21:31:37.637: E/AndroidRuntime(14050): at dalvik.system.NativeStart.main(Native Method)
09-16 21:31:37.637: E/AndroidRuntime(14050): Caused by: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.u.y(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cm.a(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cm$c.y(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cl.getLastLocation(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.internal.cm.getLastLocation(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.google.android.gms.location.LocationClient.getLastLocation(Unknown Source)
09-16 21:31:37.637: E/AndroidRuntime(14050): at com.example.Wifin.myMap.onCreate(myMap.java:51)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.Activity.performCreate(Activity.java:5133)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-16 21:31:37.637: E/AndroidRuntime(14050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-16 21:31:37.637: E/AndroidRuntime(14050): ... 11 more
09-16 21:31:37.927: D/dalvikvm(14050): GC_FOR_ALLOC freed 300K, 4% free 8930K/9284K, paused 18ms, total 18ms
09-16 21:31:38.987: I/Process(14050): Sending signal. PID: 14050 SIG: 9
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Wifin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" android:maxSdkVersion="18"/>
<permission android:name="com.example.Wifin.permission.MAPS_RECEIVE"
android:protectionLevel="signature"></permission>
<uses-permission android:name="com.example.Wifin.permission.MAPS_RECEIVE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_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_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
这是我的清单文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".SplashScreen"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@style/Theme.NoTitle" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
<activity android:name=".myMap"
android:label="@string/myMap"
/>
<receiver android:name=".WifiReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDlimK4K3HlYCjYHP4k9k6c-DWC0z-8JQ8"/>
</application>
</manifest>
答案 0 :(得分:3)
看这里
Not connected. Call connect() and wait for onConnected() to be called.
您应该致电.connect()
..然后在onConnected()
中执行该方法。
因为如果它没有连接,而你正试图访问互联网,它就会崩溃。
答案 1 :(得分:0)
我觉得错误的是mLocationClient.connect();
onConnected()
方法,因为onConnected()
是GooglePlayServicesClient.ConnectionCallbacks
接口的方法,当LocationClient被连接时调用onConnected()
谷歌服务,这是在调用mLocationclient.connect()
方法后发生的。您应该在其他地方使用mLocationClient.connect()
方法,例如onResume()
或onStart()
。