900Seconds / 900SecondsSDK-Android

Live Streaming Video SDK for Android
29 stars 21 forks source link

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference #14

Open shibli43 opened 6 years ago

shibli43 commented 6 years ago

Hello every one please help

this the Logcat

` Process: com.example.shibli.luxuryrider, PID: 23959 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

    at com.example.shibli.luxuryrider.Home.requestPickupHere(Home.java:338)
    at com.example.shibli.luxuryrider.Home.access$000(Home.java:96)
    at com.example.shibli.luxuryrider.Home$2.onClick(Home.java:217)
    at android.view.View.performClick(View.java:4848)
    at android.view.View$PerformClick.run(View.java:20300)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5682)
    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:963)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)

06-15 04:25:03.168 23959-24106/com.example.shibli.luxuryrider E/NativeCrypto: ssl=0x7f90eb6700 cert_verify_callback x509_store_ctx=0x7f7b260138 arg=0x0 06-15 04:25:03.168 23959-24106/com.example.shibli.luxuryrider E/NativeCrypto: ssl=0x7f90eb6700 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA ` And this the code

`@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);

    mService = Common.getFCMService();

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    //init storage
    storage = FirebaseStorage.getInstance();
    storageReference = storage.getReference();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    View navigationHeaderView = navigationView.getHeaderView(0);
    txtRiderName = navigationHeaderView.findViewById(R.id.txtRiderName);
    txtRiderName.setText(String.format("%s", Common.currentUser.getName()));
    txtStars = navigationHeaderView.findViewById(R.id.txtStars);
    txtStars.setText(String.format("%s", Common.currentUser.getRates()));
    imageAvatar = navigationHeaderView.findViewById(R.id.imageAvatar);

    //Load avatar
    if (Common.currentUser.getAvatarUrl() != null && !TextUtils.isEmpty(Common.currentUser.getAvatarUrl())) {
        Picasso.with(this)
                .load(Common.currentUser.getAvatarUrl())
                .into(imageAvatar);
    }

    //Maps
    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //Geo Fire
    ref = FirebaseDatabase.getInstance().getReference("Drivers");
    geoFire = new GeoFire(ref);

    //init View
    imgExpandable = (ImageView)findViewById(R.id.imgExpandable);
    mBottomSheet = BottomSheetRiderFragment.newInstance("Rider bottom sheet");
    imgExpandable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mBottomSheet.show(getSupportFragmentManager(),mBottomSheet.getTag());

        }
    });

     btnPickupRequest = (Button) findViewById(R.id.btnPickupRequest);
     btnPickupRequest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (!Common.isDriverFound)
               requestPickupHere(FirebaseAuth.getInstance().getCurrentUser().getUid());
            else
                sendRequestToDriver(Common.driverId);
        }
    });

    place_destination = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_destination);
    place_location = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_location);
    typeFilter = new AutocompleteFilter.Builder()
            .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .setTypeFilter(3)
            .build();

    //Event
    place_location.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {

            mPlaceLocation = place.getAddress().toString();

            //Remove Old Marker
            mMap.clear();

            //Add Marker at New Location
            mUserMarker = mMap.addMarker(new MarkerOptions().position(place.getLatLng())
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
                    .title("Pickup Here"));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));

        }

        @Override
        public void onError(Status status) {

        }
    });
    place_destination.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            mPlaceDestination = place.getAddress().toString();

            //Add New destination Marker
            mMap.addMarker(new MarkerOptions()
                    .position(place.getLatLng())
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.destination_marker))
                    .title("Destination"));

            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));

           // Show information in bottom
            BottomSheetRiderFragment mBottomsheet = BottomSheetRiderFragment.newInstance(mPlaceLocation);
            mBottomsheet.show(getSupportFragmentManager(), mBottomsheet.getTag());
        }

        @Override
        public void onError(Status status) {

        }
    });

    setUpLocation();

    updateFirebaseToken();
}

private void updateFirebaseToken() {
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference tokens = db.getReference(Common.token_tb1);

    Token token = new Token(FirebaseInstanceId.getInstance().getToken());
    tokens.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .setValue(token);

}

private void sendRequestToDriver(String driverId) {
    DatabaseReference tokens = FirebaseDatabase.getInstance().getReference(Common.token_tb1);

    tokens.orderByKey().equalTo(driverId)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) {
                        Token token = postSnapShot.getValue(Token.class);

                        //Make raw payload
                        String json_lat_lng = new Gson().toJson(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
                        String riderToken = FirebaseInstanceId.getInstance().getToken();
                        Notification data = new Notification(riderToken, json_lat_lng);
                        Sender content = new Sender(token.getToken(), data);

                        mService.sendMessage(content)
                                .enqueue(new Callback<FCMResponse>() {
                                    @Override
                                    public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                                        if (response.body().success == 1)
                                            Toast.makeText(Home.this, "Request sent", Toast.LENGTH_SHORT).show();
                                        else
                                            Toast.makeText(Home.this, "Failed", Toast.LENGTH_SHORT).show();
                                    }

                                    @Override
                                    public void onFailure(Call<FCMResponse> call, Throwable t) {
                                        Log.e("ERROR", t.getMessage());

                                    }
                                });
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
}

private void requestPickupHere(String uid) {
    DatabaseReference dbRequest = FirebaseDatabase.getInstance().getReference(Common.pickup_request_tb1);
    GeoFire mGeoFire = new GeoFire(dbRequest);
    mGeoFire.setLocation(uid, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));

    if (mUserMarker.isVisible())
        mUserMarker.remove();

    //Add new Marker
    mUserMarker = mMap.addMarker(new MarkerOptions()
            .title("Pickup Here")
            .snippet("")
            .position(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()))
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    mUserMarker.showInfoWindow();

    btnPickupRequest.setText("Getting your Driver...");

    findDriver();

}

`