【问题标题】:Fused Location Crash融合位置崩溃
【发布时间】:2013-12-11 11:11:48
【问题描述】:

嗨,我一直在尝试让融合位置工作,但总是让应用程序崩溃,谁能告诉我 wgy 吗?坦克你

收到此错误:

12-11 02:08:54.525:E/AndroidRuntime(5972):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.teste/com.example.teste.MainActivity}:java.lang。 IllegalStateException:未连接。调用 connect() 并等待 onConnected() 被调用。

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.teste"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.teste.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.version"       android:value="@integer/google_play_services_version" />
</application>

</manifest>

主要:

package com.example.teste;
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 android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{

LocationClient mLocationClient;
Location mCurrentLocation;
TextView v1,v2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int resultCode =
            GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    // If Google Play services is available
    if (ConnectionResult.SUCCESS == resultCode) {
        // In debug mode, log the status
        Toast.makeText(this, "Serciços Google Play disponiveis",     Toast.LENGTH_LONG).show(); }
    mLocationClient = new LocationClient(this, this, this);
    mCurrentLocation = mLocationClient.getLastLocation();
    setContentView(R.layout.activity_main);
    v1 = (TextView) findViewById(R.id.textView1);
    v2 = (TextView) findViewById(R.id.textView2);

    v1.setText(Double.toString(mCurrentLocation.getLatitude()));
    v2.setText(Double.toString(mCurrentLocation.getLongitude()));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
     mLocationClient.connect();
}


@Override
protected void onStop() {
     mLocationClient.disconnect();
    super.onStop();
}


@Override
public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}


@Override
public void onConnected(Bundle arg0) {
    Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();

}


@Override
public void onDisconnected() {
     Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();

}

}

布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="21dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="32dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

【问题讨论】:

    标签: java android


    【解决方案1】:

    正如错误消息所说,在调用onConnected() 之前,您不能调用mLocationClient.getLastLocation()。将该调用(以及任何依赖它的调用)移至您的 onConnected() 方法。

    【讨论】:

    • 我在谷歌文档的指导下,但他们只有部分代码,所以可能是这样,但我之前将代码移动到 onConnected() 并且没有返回值,(提取纬度和经度)在 lynda.com 示例的指导下,该示例在用户创建的特定方法上使用了 onConnect() 之外的方法,是否更改了 api(lynda.com 视频来自今年 3 月)或者还有其他需要做的事情那样工作?坦克你
    • 没关系,我完全忘记了我几乎所有的应用程序和服务都限制在后台访问 GPRS 和播放服务也被阻止。坦克你
    • 但是如果在 onConnected 之外有任何方法可以做到这一点,请告诉我。再次坦克你
    • @user3089297 - 只要onConnected 已被调用,您就可以从您活动中的任何位置调用getLastLocation。您可能希望在onConnectedonDisconnected 中将boolean 变量设置为truefalse - 这样您就可以在调用getLastLocation 之前检查该变量。
    • 但是我已经连接了,连接onStart 断开onStop,我什至尝试调用mLLocationClient.connect();在剩余代码之前的 onCreate 上,但错误与我发布的相同,(我认为需要再试一次,看看是否不仅仅是被阻止的服务)再次坦克你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多