2016-06-05 9 views
0

Meine Anwendung stürzt ab, sobald sie in Nexus 6 geöffnet wird. Das Problem ist mit getMapAsync().Aktivität kann nicht gestartet werden ... Versuch, virtuelle Methode aufzurufen ... MapFragment.getMapAsync

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { 

    GoogleMap mMap; //member variable 
    private static final int ERROR_DIALOG_REQUEST = 9001; //constant to request certain king of dialog box definition from google play services lib 
    private static final double 
      AJAX_LAT = 43.851063, 
      AJAX_LNG = -79.019737; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (servicesOK()) { 
      setContentView(R.layout.activity_main); 

      if (initMap()) { 
       Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show(); 
       gotoLocation(AJAX_LAT, AJAX_LNG, 13); 

       //as of now user must grand permissions to app from phone settings 
       try { 
        mMap.setMyLocationEnabled(true); 
       } catch (SecurityException e) { 
        Toast.makeText(this, "My Location not enabled!", Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       Toast.makeText(this, "Map not connected!", Toast.LENGTH_SHORT).show(); 
      } 
     } else { 
      Toast.makeText(this, "Services not OK!", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    //get a reference to the map object 
    public boolean initMap() { 
     if (mMap == null) { 
      MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); 
     } 
     return (mMap != null); 
    } 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

    } 

    //check to see if the google services are ok 
    public boolean servicesOK() { 

     GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); 
     int isAvailable = googleAPI.isGooglePlayServicesAvailable(this); 
     // handle 3 different possibility 
     if (isAvailable == ConnectionResult.SUCCESS) { //everything ok, user can make mapping request 
      return true; 
     } else if (googleAPI.isUserResolvableError(isAvailable)) { //error user can do something about 
      Dialog dialog = googleAPI.getErrorDialog(this, isAvailable, ERROR_DIALOG_REQUEST); 
      dialog.show(); //google play services lib delivers correct dialog box telling user what to do 
     } else { //something wrong, user cant do anything about 
      Toast.makeText(this, "Can't connect to mapping service", Toast.LENGTH_SHORT).show(); 
     } 
     return false; 
    } 
} 

Unten finden Sie die Fehler i

erhalten
Process: com.example.suhail.onmyway, PID: 27425 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.suhail.onmyway/com.example.suhail.onmyway.MainActivity}: 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: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.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.suhail.onmyway.MainActivity.initMap(MainActivity.java:89) 
    at com.example.suhail.onmyway.MainActivity.onCreate(MainActivity.java:47) 
    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) 

ich habe eine Schaltfläche in activity_main.xml, die einmal öffnet sich ein weiteres Layout angeklickt, wo ein Fragment

+1

Normalerweise verwenden Sie mit 'AppCompatActivity'' SupportMapFragment' und 'getSupportFragmentManager()'. Darüber hinaus möchten Sie möglicherweise Ihr 'activity_main'-Layout veröffentlichen. – CommonsWare

+0

Ich habe SupportMapFragment verwendet mapFragment = (SupportMapFragment) getSupportFragmentManager(). FindFragmentById (R.id.map); mMap = mapFragment.getMap(); vorher, aber getMap() ist jetzt veraltet –

+0

Mögliches Duplikat von [Was ist eine NullPointerException, und wie behebe ich es?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and- how-do-i-fix-it) – Vucko

Antwort

0

Put setContentView aus, wenn Block enthalten ist Es stürzt ab Zeile 89 Was ist Code dieser Zeile?

------------ ------------ aktualisieren

Verwenden Sie:

  GoogleMap map = ((WorkaroundMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
      final LatLng GOOGLE_MAP_POSITION = new LatLng(lat, lng); 
      Log.d("GOOGLE_MAP", "lat : " + lat + " , lng : " + lng); 
      if (map != null) { 
       map.clear(); 
       map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       Marker marker = map.addMarker(new MarkerOptions().position(GOOGLE_MAP_POSITION) 
         .title("Title") 
         .snippet("Content Goes Here") 
         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))); 
      } 
      MapsInitializer.initialize(this); 
      // Move the camera instantly to target with a zoom of 14. 
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(GOOGLE_MAP_POSITION, 50)); 
      // Zoom in, animating the camera, Change 14 to Zoom in And Zoom Out 
      map.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null); 

      int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
      if (resultCode == ConnectionResult.SUCCESS){ 
       Log.e("DETAIL_ACTIVITY", "Google Play Services Util Available SUCCESS : "+resultCode); 
      }else{ 
       Log.e("DETAIL_ACTIVITY", "Google Play Services Util Available Problem : "+resultCode); 
       GooglePlayServicesUtil.getErrorDialog(resultCode, this, 1); 
      } 

Auch WorkaroundMapFragment ist eine benutzerdefinierte Klasse, die Lassen Sie die Karte innerhalb von ScrollView verwenden

Um es zu verwenden check it out.

+0

Zeile 89 MapFragment mapFragment = (MapFragment) getFragmentManager(). findFragmentById (R.id.map); mapFragment.getMapAsync (this); –

+0

Überprüfen Sie mein Update ... – AndroSco

0

Wahrscheinlich Ihre mapFragment null ist, auch falsche getMapAsync Verwendung, müssen Sie alle verwenden, die erst, nachdem Sie die Karte erhalten zur Karte gehört, so müssen Sie erst nach dieser Aufruf handeln zurückgerufen wird:

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

} 

add alle nächsten Initialisierungen in runnable und

es nach
mMap = googleMap; 
0

du activity_main.xml

<fragment 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
zu Ihrer hinzufügen müssen anrufen
+0

Ich habe eine Schaltfläche in activity_main.xml, die einmal geklickt öffnet ein anderes Layout, wo ein Fragment enthalten ist

+0

Können Sie diesen Code bitte zur Beschreibung hinzufügen? –

Verwandte Themen