Android Map应用程序在启动时崩溃

时间:2017-03-01 18:31:03

标签: java android google-maps maps

通过Android Studio使用Google Maps API时,我一直有这个问题。它编译很好,没有错误,但在我的设备上运行时,它总是在启动时崩溃。我使用的是华为P90 Lite(Marshmallow 6.0)。

非常感谢你的帮助! Joe S。

以下是代码:

MapsActivity.java

package com.example.jonathan.parq01;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.support.annotation.RequiresApi;
import  android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.Date;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

private SensorManager sensorManager;
private Sensor accelerometer;
private float last_x, last_y, last_z;

long lastUpdate = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    // setup the accelerometer
    sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
    accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sensorManager.registerListener((SensorEventListener) this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

@RequiresApi(api = Build.VERSION_CODES.N)
public void OnSensorChanged(SensorEvent event) {
    Sensor mySensor = event.sensor;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float x = event.values[0]; // x value
        float y = event.values[1]; // y value
        float z = event.values[2]; // z value

        long curTime = System.currentTimeMillis();

        if (Math.abs(curTime - lastUpdate) > 2000)
        {
            SimpleDateFormat date = new SimpleDateFormat("dd-MM-yyyy");
            String currentDateTime = date.format(new Date());

            lastUpdate = curTime;

            if (Math.abs(last_x - x) > 10)
            {
                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(37.23062, -80.42178))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
                        .title("Hey you moved the x axis" + currentDateTime));
            }

            if (Math.abs(last_y - y) > 10)
            {
                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(37.26062, -80.42188))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                        .title("Hey you moved the y axis" + currentDateTime));
            }

            if (Math.abs(last_z - z) > 10)
            {
                mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(37.24062, -80.43178))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                        .title("Hey you moved the z axis" + currentDateTime));
            }

            last_x = x;
            last_y = y;
            last_z = z;
        }
    }
}

public void OnAccuracyChanged(Sensor sensor, int accuracy) {

}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(new LatLng(37.229, 80.424)).title("Virginia Tech"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.229, 80.424), 14.9f));
}
}

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jonathan.parq01.MapsActivity" />

我还将Google Maps API密钥添加到相应的xml文件中。

AndroidManifest.xml

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

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

堆栈追踪:

03-01 15:41:16.021 9132-9132/? I/art: Late-enabling -Xcheck:jni
03-01 15:41:16.038 9132-9138/? I/art: Debugger is no longer active
03-01 15:41:16.079 9132-9132/? W/System: ClassLoader referenced unknown        path: /data/app/com.example.jonathan.parq01-2/lib/arm64
03-01 15:41:16.086 9132-9132/? I/InstantRun: Instant Run Runtime started. Android package is com.example.jonathan.parq01, real application class is null.
03-01 15:41:16.383 9132-9132/? W/System: ClassLoader referenced unknown path: /data/app/com.example.jonathan.parq01-2/lib/arm64
03-01 15:41:16.415 9132-9132/? I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
03-01 15:41:16.526 9132-9132/? I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
03-01 15:41:16.603 9132-9132/? I/zzai: Making Creator dynamically
03-01 15:41:16.668 9132-9132/? W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000027/n/arm64-v8a
03-01 15:41:16.679 9132-9132/? I/System.out: [/system/bin/getprop,    debug.mapview.logs]
03-01 15:41:16.679 9132-9132/? I/System.out: null
03-01 15:41:16.679 9132-9132/? I/System.out: null
03-01 15:41:16.679 9132-9132/? I/System.out: Calling by::className:com.google.maps.api.android.lib6.common.q  MethodName:a
03-01 15:41:16.704 9132-9132/? I/Google Maps Android API: Google Play services client version: 10298000
03-01 15:41:16.708 9132-9132/? I/Google Maps Android API: Google Play services package version: 10298448
03-01 15:41:16.708 9132-9132/? I/System.out: [/system/bin/getprop, debug.mapview.renderer]
03-01 15:41:16.708 9132-9132/? I/System.out: null
03-01 15:41:16.708 9132-9132/? I/System.out: null
03-01 15:41:16.709 9132-9132/? I/System.out: Calling by::className:com.google.maps.api.android.lib6.common.q  MethodName:a
03-01 15:41:16.732 9132-9132/? I/System.out: [/system/bin/getprop, debug.mapview.streetview]
03-01 15:41:16.732 9132-9132/? I/System.out: null
03-01 15:41:16.732 9132-9132/? I/System.out: null
03-01 15:41:16.732 9132-9132/? I/System.out: Calling by::className:com.google.maps.api.android.lib6.common.q  MethodName:a
03-01 15:41:16.781 9132-9132/? I/System.out: [/system/bin/getprop, debug.mapview.gmmserver]
03-01 15:41:16.781 9132-9132/? I/System.out: null
03-01 15:41:16.781 9132-9132/? I/System.out: null
03-01 15:41:16.782 9132-9132/? I/System.out: Calling by::className:com.google.maps.api.android.lib6.common.q  MethodName:a
03-01 15:41:16.952 9132-9157/? I/System: core_booster, getBoosterConfig = false
03-01 15:41:16.982 9132-9157/? I/System: core_booster, getBoosterConfig = false
03-01 15:41:17.316 9132-9132/? I/Process: Sending signal. PID: 9132 SIG: 9

2 个答案:

答案 0 :(得分:0)

您需要获取用于制作Google地图应用的SHA1密钥。您可以通过在命令提示符下键入以下命令来获取您的sha1密钥: -

keytool -list -v -keystore“%USERPROFILE%.android \ debug.keystore”-alias androiddebugkey -storepass android -keypass android

答案 1 :(得分:0)

也许创建一个密钥库并将其放置在需要的地方。 ~我的 Android Studio 有类似的东西,它总是在错误的位置寻找密钥库文件。

所需的文件仍然存在,但需要从它引用的文件夹“Above”中复制。也就是说,在父文件夹的目录中是文件,Android Studio 在这个文件夹的子文件夹中查找(嵌套错误)

你可以把正确的文件,我的是(~.android.android\debug.keystore),然后复制到正确的目录,app~.android.android\debug.keystore.~

这对我帮助很大,希望对你也有帮助。

来自各种 Stack Overflow 研究,以反复试验告终!