2017-03-01 103 views
0

我通過Android Studio使用Google地圖API時一再出現此問題。它編譯好,沒有錯誤,但在我的設備上運行時,它始終在啓動時崩潰。我正在使用華爲P90 Lite(棉花糖6.0)。Android地圖應用在啓動時在設備上崩潰

非常感謝您的幫助! 喬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" /> 

我還添加了谷歌地圖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 
+4

你可以粘貼堆棧跟蹤嗎? –

+0

謝謝,我在上面添加了它。 –

回答

0

你需要獲得SHA1鍵用於製作谷歌地圖app.you可以通過在命令提示符處鍵入以下命令得到你的SHA1密鑰: -

keytool -list -v -keystore「%USERPROFILE%.android \ debug.keystore」-alias androiddebugkey -storepass android -keypass android

+0

謝謝,但運行此命令時,出現以下錯誤: 「java.lang.Exception:密鑰庫文件不存在」 –

+0

keytool -list -v -keystore mystore.keystore(使用此命令) –

+0

對不起,但我仍然收到相同的錯誤信息。 –