我已经查看过这个问题的各种其他Stack Overflow帖子,但似乎无法弄明白。我加载使用Google API的DisplayInformation类后,我的应用就会崩溃。
这是Logcat:
03-20 21:23:02.233 2594-2594/? I/art: Late-enabling -Xcheck:jni
03-20 21:23:02.262 2594-2600/? E/art: Failed sending reply to debugger: Broken pipe
03-20 21:23:02.262 2594-2600/? I/art: Debugger is no longer active
03-20 21:23:02.340 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:02.783 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:02.848 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:02.919 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:02.987 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.060 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.136 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.194 2594-2594/? W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.255 2594-2594/com.example.matt.ignitecs W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.346 2594-2594/com.example.matt.ignitecs W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.406 2594-2594/com.example.matt.ignitecs W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.example.matt.ignitecs-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.matt.ignitecs-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
03-20 21:23:03.409 2594-2594/com.example.matt.ignitecs W/System: ClassLoader referenced unknown path: /data/app/com.example.matt.ignitecs-2/lib/arm
03-20 21:23:03.413 2594-2594/com.example.matt.ignitecs I/InstantRun: Starting Instant Run Server for com.example.matt.ignitecs
03-20 21:23:03.749 2594-2594/com.example.matt.ignitecs W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-20 21:23:03.984 2594-2680/com.example.matt.ignitecs D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-20 21:23:04.016 2594-2680/com.example.matt.ignitecs I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
03-20 21:23:04.018 2594-2680/com.example.matt.ignitecs I/OpenGLRenderer: Initialized EGL, version 1.4
03-20 21:23:19.756 2594-2594/com.example.matt.ignitecs D/AndroidRuntime: Shutting down VM
03-20 21:23:19.757 2594-2594/com.example.matt.ignitecs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.matt.ignitecs, PID: 2594
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matt.ignitecs/com.example.matt.ignitecs.DisplayInformation}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
at com.google.android.gms.internal.zzaat.zzb(Unknown Source)
at com.google.android.gms.internal.zzary.requestLocationUpdates(Unknown Source)
at com.example.matt.ignitecs.DisplayInformation.onCreate(DisplayInformation.java:42)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
代码:
package com.example.matt.ignitecs;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
public class DisplayInformation extends AppCompatActivity
implements OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks, LocationListener
{
private GoogleApiClient mGoogleApiClient;
TextView txtLocCoarse;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_information);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
txtLocCoarse = (TextView) findViewById(R.id.txtLocCoarse);
try {
LocationRequest mCoarseLocationRequest = new LocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mCoarseLocationRequest, this);
Location mCurrentLocation;
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
txtLocCoarse.setText(mCurrentLocation.getLatitude() + ", " + mCurrentLocation.getLongitude());
} catch (SecurityException e) {
}
} // end onCreate
@Override
public void onConnectionFailed(ConnectionResult result) {
// An unresolvable error has occurred and a connection to Google APIs
// could not be established. Display an error message, or handle
// the failure silently
// ...
}
@Override
public void onConnected(Bundle connectionHint) {
//your code goes here
}
@Override
public void onConnectionSuspended(int cause) {
//your code goes here
}
@Override
public void onLocationChanged(Location loc)
{
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
} // end class
用于DisplayInformation的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="#006633"
tools:context="com.example.matt.ignitecs.DisplayInformation">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="341dp"
android:layout_height="306dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="245dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location: "
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/txtLocCoarse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111.11 111.11"
tools:layout_editor_absoluteX="91dp"
tools:layout_editor_absoluteY="16dp" />
</android.support.constraint.ConstraintLayout>
如果需要提供其他任何东西,请询问。