Вызов NullPointerException выделил ошибку в Android Studio при переходе от действия к действию Google Maps. - PullRequest
0 голосов
/ 12 октября 2018

Я создаю приложение, которое переместится в действие карты при нажатии кнопки. Здесь я беру Широту и Долготу из базы данных. Я не получаю сообщение об ошибке, но карта не отображается. В MapsActivity getMapASync () выделяется ивозможность для NullPointerException отображается

Кстати, я использую SDK 28

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final int PERMISSION_REQUEST_CODE = 200;
    public static final String LATITUDE_DATA = "Latitude";
    public static final String LONGITUDE_DATA = "Longitude";

    private DocumentReference mDocRef = FirebaseFirestore.getInstance().collection("Waviour").document("Waviour");
    private String FIRE_LOG = "Fire_log";

    public static class Globals {
        public static Double Latitude;
        public static Double Longitude;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION,WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
        } else {
            // Permission has already been granted
        }
    }
    public void getData(View view){
        mDocRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                 if (documentSnapshot.exists()){
                     String Latitude = documentSnapshot.getString(LATITUDE_DATA);
                     String Longitude = documentSnapshot.getString(LONGITUDE_DATA);
                     Globals.Latitude = Double.parseDouble(Latitude);
                     Globals.Longitude = Double.parseDouble(Longitude);
                     Snackbar.make(findViewById(R.id.LayoutConstraint),"Your Nearest Cycle stall is at " + Latitude + Longitude,Snackbar.LENGTH_SHORT)
                             .show();
                 } else if (documentSnapshot.getString(LATITUDE_DATA) == null && documentSnapshot.getString(LONGITUDE_DATA) == null){
                     Snackbar.make(findViewById(R.id.LayoutConstraint),R.string.Error1,Snackbar.LENGTH_SHORT)
                             .show();
                 }
            }
        });
        startActivity(new Intent(this, MapsActivity.class));
    }
}

MapsActivity.java

package com.divyateja.name;

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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    public GoogleMap mMap;

    @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);
    }


    /**
     * 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;
        LatLng sydney = new LatLng(MainActivity.Globals.Latitude, MainActivity.Globals.Longitude);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".MapsActivity" />

activity_main.xml

<?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:id="@+id/LayoutConstraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:background="@drawable/button_round"
        android:onClick="getData"
        android:text="@string/textbtn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

Журнал Я не смог прикрепить журнал как текст, поэтому вот он

https://raw.githubusercontent.com/Divyateja04/Waviour/master/Logcat

...