2017-12-20 2 views
0

Ich versuche, eine App mit einer einfachen Benutzeroberfläche zu erstellen. Es besteht aus einer Aktivität und besteht aus 2 Schaltflächen, einer Textansicht und einem Google Maps-Fragment, das die Hälfte des Bildschirms ausfüllt. Ich habe das Layout mithilfe von Drag & Drop-Elementen aus Android Studio erstellt.Nullzeiger bei Verwendung von getMapAsync erhalten

Das Problem ist, dass, wenn ich versuche, es zu laufen, ich folgende Fehlermeldung erhalten: jjava.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

Diese meine Tätigkeit ist:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{ 

RequestQueue requestQueue; 
Button serviceButton; 
TextView textViewService; 
SensorResponse responseJson; 
private GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestQueue = Volley.newRequestQueue(this); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView2); 
    mapFragment.getMapAsync(this); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    textViewService = findViewById(R.id.textViewServiceOutput); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@Override 
public void onMapReady(GoogleMap map) { 

    googleMap = map; 

    setUpMap(); 

} 
public void setUpMap(){ 

    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    //googleMap.setMyLocationEnabled(true); 
    googleMap.setTrafficEnabled(true); 
    googleMap.setIndoorEnabled(true); 
    googleMap.setBuildingsEnabled(true); 
    googleMap.getUiSettings().setZoomControlsEnabled(true); 
}} 

ich den Fehler gegoogelt habe und haben die Lösungen ausprobiert dass ich zum Beispiel auf SupportMapFragment stieß, aber es funktioniert immer noch nicht.

Folgende ist mein Layout-Datei:

<?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" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.tarikh.myapplication.MainActivity" 
tools:showIn="@layout/activity_main"> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="47dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:onClick="callTheService" 
    android:text="Call Service" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/mapButton" /> 

<TextView 
    android:id="@+id/textViewServiceOutput" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="128dp" 
    android:layout_marginRight="128dp" 
    android:layout_marginTop="16dp" 
    android:text="Parking Bay Checker" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

<Button 
    android:id="@+id/mapButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:onClick="callTheMap" 
    android:text="Call Map" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/textViewServiceOutput" /> 

<com.google.android.gms.maps.MapView 
    android:id="@+id/mapView2" 
    android:layout_width="match_parent" 
    android:layout_height="244dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="44dp" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/button3" /> 

</android.support.constraint.ConstraintLayout> 

EDIT: Dank für die Eingabe, nach ein bisschen ein Zwicken, habe ich es geschafft, es arbeiten zu lassen. Ich ersetzte die Karte auf meiner Tätigkeit Layout mit den folgenden:

<fragment 
    android:id="@+id/mapFrag" 
    class="com.google.android.gms.maps.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="329dp" 
    android:layout_marginTop="164dp" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 
+0

Können Sie das Layout-Datei aktualisieren verwenden? – R2R

+0

@ R2R aktualisiert die Frage mit meiner Layout-Datei – dfqupwndguerrillamailde

Antwort

1

Unter Anweisung den Wert null zurück, da Sie nicht MapFragment in Aktivität hinzugefügt haben, können Sie MapView

(MapFragment) getFragmentManager().findFragmentById(R.id.mapView2) 
1

hinzugefügt haben In versuchen Layout MapFragment .Ihre zuordnen MapView MapFragment deshalb null Ursache

<fragment 
    android:id="@+id/mapView2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    /> 
zu verwenden

Hier können Sie sehen, wo wir MapView und MapFragment

https://stackoverflow.com/a/34626302/3505534

+0

Danke für den Vorschlag, es hat funktioniert. Muss ein Fragment in einem Container residieren (z. B. relatives Layout) oder kann ein Fragment auf seinem On ohne Container existieren? – dfqupwndguerrillamailde

Verwandte Themen