呼叫位置更新会导致Android应用崩溃

时间:2016-04-07 15:00:33

标签: android location-services

Stackoverflow,我需要你的帮助......我开始学习Android应用程序编程,并且我很难用这个:

我试图在Android Studio上稍微重现this example,但在调用LocationServices.FusedLocationApi.requestLocationUpdates()时我一直挂起。我已经设置了我的代码,因此只有在按下UI按钮时才会运行。我点击它,我的应用程序说"不幸的是' myApp'已停止"。

这是我的MainActivity.java



package com.ricardohalfeld.mygpsapi22;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;


public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener{

    //Variable Declaration
    protected GoogleApiClient mGoogleApiClient;
    protected LocationRequest mLocationRequest;

    Location mLastLocation;

    TextView mLatitudeTextView;
    TextView mLongitudeTextView;
    TextView mDataOriginTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getHoldOfTextViews();

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                buildGoogleApiClient();
            }
        }
        else {
            buildGoogleApiClient();
        }
    }

    //Activity related methods
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    void getHoldOfTextViews(){
        mLatitudeTextView = (TextView) findViewById(R.id.latTextView);
        mLongitudeTextView = (TextView) findViewById(R.id.lonTextView);
        mDataOriginTextView = (TextView) findViewById(R.id.dataOriginTextView);
    }

    //Google API Client related methods
    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onConnected(Bundle bundle) {
        requestLastLocation();
        createLocationRequest();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {

    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        //API23 requirement
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            startLocationUpdates();
        }
    }

    void requestLastLocation(){
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if (mLastLocation != null) {
            mLatitudeTextView.setText(String.valueOf(mLastLocation.getLatitude()));
            mLongitudeTextView.setText(String.valueOf(mLastLocation.getLongitude()));

            mDataOriginTextView.setText("Data originated from requestLastLocation();");
        }
        else {
            mDataOriginTextView.setText("Location is null");
        }
    }

    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        mDataOriginTextView.setText("Data originated from startLocationUpdates();");
    }

    public void testMethodWithButton(View v){ //Passing the View is required for the button to work

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {//API23 requirement
            startLocationUpdates();
            mDataOriginTextView.setText("Permission Granted in Manifest");
        }
        else {
            mDataOriginTextView.setText("Permission NOT Granted in Manifest");
        }
    }

}




这就是我的content_main.xml:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ricardohalfeld.mygpsapi22.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="GPS Data"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Latitude"
        android:id="@+id/latTextView"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Longitude"
        android:id="@+id/lonTextView"
        android:layout_below="@+id/latTextView"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Valid for API22"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Request Location Updates"
        android:id="@+id/callMethodButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="testMethodWithButton"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="No Data Generated"
        android:id="@+id/dataOriginTextView"
        android:layout_below="@+id/lonTextView"
        android:layout_alignParentStart="true" />
</RelativeLayout>
&#13;
&#13;
&#13;

从这篇文章之前的研究中,我相信你也想看到这些:

我的AndroidManifest.xml

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ricardohalfeld.mygpsapi22">

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

   <!--Required for Location awareness-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!--<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>-->

</manifest>
&#13;
&#13;
&#13;

为了把事情搞定,我的Build.Gradle(模块:应用程序)

&#13;
&#13;
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.ricardohalfeld.mygpsapi22"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.google.android.gms:play-services:8.4.0'
}
&#13;
&#13;
&#13;

添加堆栈跟踪似乎,至少对于我没有经验的眼睛,就像问题一样:

04-13 15:37:22.882 31046-31046/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.ricardohalfeld.mygpsapi22, PID: 31046
                                               java.lang.IllegalStateException: Could not execute method for android:onClick
                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                   at android.view.View.performClick(View.java:5254)
                                                   at android.view.View$PerformClick.run(View.java:21179)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:145)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6837)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                Caused by: java.lang.reflect.InvocationTargetException
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                   at android.view.View.performClick(View.java:5254) 
                                                   at android.view.View$PerformClick.run(View.java:21179) 
                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                   at android.os.Looper.loop(Looper.java:145) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6837) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
                                                Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
                                                   at com.google.android.gms.common.api.internal.zzj.zzb(Unknown Source)
                                                   at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
                                                   at com.ricardohalfeld.mygpsapi22.MainActivity.startLocationUpdates(MainActivity.java:130)
                                                   at com.ricardohalfeld.mygpsapi22.MainActivity.testMethodWithButton(MainActivity.java:137)
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                   at android.view.View.performClick(View.java:5254) 
                                                   at android.view.View$PerformClick.run(View.java:21179) 
                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                   at android.os.Looper.loop(Looper.java:145) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6837) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您已经错过了对GoogleApiClient的connect()来电。

您只需将呼叫添加到buildGoogleApiClient()功能:

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

在请求位置更新之前,还要向isConnected()添加支票:

public void testMethodWithButton(View v){ //Passing the View is required for the button to work

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {//API23 requirement
        //don't call startLocationUpdates if mGoogleApiClient is not connected:
        if (mGoggleApiClient.isConnected()) {
            startLocationUpdates();
        }
        mDataOriginTextView.setText("Permission Granted in Manifest");
    }
    else {
        mDataOriginTextView.setText("Permission NOT Granted in Manifest");
    }
}