Google Maps Android应用无法正常运行

时间:2016-03-05 15:51:02

标签: java android google-maps

我已经基于GoogleMaps创建了这个Android应用程序,但是当我尝试在我的智能手机(华为P8 Lite)上运行它时,有时它运行正常,但有时(大部分时间)我收到错误说“不幸的是,应用程序停止了“。我不知道为什么它可以正常运行几次然后它停止工作并且不会打开....

这是AndroidManifest.xml文件:

public class BrowseableActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    Toast.makeText(this,"called",Toast.LENGTH_LONG).show();
    Intent intent= new Intent(this, MainActivity.class);
    startActivity(intent);
  }
}

这是MapsActivity.java文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.francydarkcool.maps">

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

<string android:name="xxxxx">11111</string>

<permission
    android:name="com.example.francydarkcool.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.francydarkcool.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


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

    <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>

</manifest>

和reeeealy大logcat:

package com.example.francydarkcool.maps;

import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;

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

public class MapsActivity extends FragmentActivity {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the MapFragment and get notified when the map is ready to be used.
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            mMap.setMyLocationEnabled(true);
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }

        }
    });

    setUpMapIfNeeded();
}

protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {
    //Do a null check to see if the map is not already instantiated
    if (mMap == null)
        //try to obtain the map from the MapFragment.
         {
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
                if (ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                mMap.setMyLocationEnabled(true);
                // Check if we were successful in obtaining the map.
                if (mMap != null) {
                    setUpMap();
                }

            }
        });
    }
    //check if successful
    if (mMap != null) {
        setUpMap();
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(45.402850, 25.518595)).title("unu"));
    mMap.addMarker(new MarkerOptions().position(new LatLng(45.402856, 25.518608)).title("doi"));
    mMap.addMarker(new MarkerOptions().position(new LatLng(45.402834, 25.518772)).title("trei"));

    //Enable MyLocation Layer of Google Map
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mMap.setMyLocationEnabled(true);

    //Get LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    //Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    //Get current location
    Location myLocation = locationManager.getLastKnownLocation(provider);

    if(myLocation != null){
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        }
    else
    {
        Location   getLastLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        double longitude = getLastLocation.getLongitude();
        double latitude = getLastLocation.getLatitude();
    }

    //set map type
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    //Get latitude of the current location
    double latitude = myLocation.getLatitude();

    //Get longitude of the current location
    double longitude = myLocation.getLongitude();

    //Create a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    //Show the current location in Google Maps
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    //Zoom in GoogleMap
    mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
}
}

activity_maps.xml文件:

03-07 13:18:59.900 30222-30222/com.example.francydarkcool.maps E/ZipFileCache: init failed when open zip file.
03-07 13:18:59.900 30222-30222/com.example.francydarkcool.maps E/ZipFileCache: init failed when open zip file.
03-07 13:19:00.080 30222-30222/com.example.francydarkcool.maps E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.francydarkcool.maps, PID: 30222
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.francydarkcool.maps/com.example.francydarkcool.maps.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
                                                                                 at android.app.ActivityThread.access$1200(ActivityThread.java:152)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5538)
                                                                                 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:960)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
                                                                                 at com.example.francydarkcool.maps.MapsActivity.onCreate(MapsActivity.java:29)
                                                                                 at android.app.Activity.performCreate(Activity.java:6013)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466) 
                                                                                 at android.app.ActivityThread.access$1200(ActivityThread.java:152) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5538) 
                                                                                 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:960) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

strings.xml文件:

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

0 个答案:

没有答案