来自不同类

时间:2018-06-17 07:47:24

标签: android firebase android-maps

尝试从片段中的其他类执行onclicklistener方法。我有这个错误。我是编程新手,有人会帮我解决这个问题。

这是BottomSheetMotoristFragment.java上的方法

bRequest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MainActivity mainAct = new MainActivity();


            if (!Common.isDriverFound)
                mainAct.requestPickUpHere(FirebaseAuth.getInstance().getCurrentUser().getUid());

            else
                Common.sendRequestToDriver(Common.driverId,mFcmService,mainAct.getBaseContext(),Common.mLastLocation);



        }
    });

这是我想从MainActivity.java调用的方法

public void requestPickUpHere(String uid) {

    DatabaseReference dbRequest = FirebaseDatabase.getInstance().getReference(Common.pickup_request_tbl);
    GeoFire mGeoFire = new GeoFire(dbRequest);
    mGeoFire.setLocation(uid,new GeoLocation(Common.mLastLocation.getLatitude(),Common.mLastLocation.getLongitude()));

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


    //Add new marker

   mUserMarker =  mMap.addMarker(new MarkerOptions()
           .title("Pickup Here")
           .snippet("")
           .position(new LatLng(Common.mLastLocation.getLatitude(),Common.mLastLocation.getLongitude()))
           .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));

    mUserMarker.showInfoWindow();

    bRequest.setText("Getting your driver......");

    findDriver();



} 

这是错误

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.maps.model.Marker.isVisible()' on a null object reference
    at hushtagstudios.com.towme.MainActivity.requestPickUpHere(MainActivity.java:888)
    at hushtagstudios.com.towme.BottomSheetMotoristFragment$1.onClick(BottomSheetMotoristFragment.java:91)

1 个答案:

答案 0 :(得分:1)

根据您的提供,有一个嫌疑人:

public void requestPickUpHere(String uid) {

    DatabaseReference dbRequest = FirebaseDatabase.getInstance().getReference(Common.pickup_request_tbl);
    GeoFire mGeoFire = new GeoFire(dbRequest);
    mGeoFire.setLocation(uid,new GeoLocation(Common.mLastLocation.getLatitude(),Common.mLastLocation.getLongitude()));

    // IT IS NOT CLEAR WHERE YOU GET mUserMarker (probably it is a field of a class)
    // BUT LOOKS LIKE AT THIS POINT IT IS NOT INSTANTIATED - it is null !
    if (mUserMarker.isVisible())
        mUserMarker.remove();
    // BELOW THERE IS A PIECE OF CODE THAT SHOULD FIX YOUR ERROR


   //Add new marker
   // ONE MORE COMMENT HERE: that's probably the place where you first set the value to mUserMarker
   // maybe it make sense to check for visibility after this line,
   // don't know if it is your case. just an assumption 
   mUserMarker =  mMap.addMarker(new MarkerOptions()
           .title("Pickup Here")
           .snippet("")
           .position(new LatLng(Common.mLastLocation.getLatitude(),Common.mLastLocation.getLongitude()))
           .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
    mUserMarker.showInfoWindow();
    bRequest.setText("Getting your driver......");
    findDriver();

}

请检查我在您的代码中留下的评论。

要在我评论的行中修复NPE,请尝试此操作(基本上只检查null):

if (mUserMarker != null && mUserMarker.isVisible()) {
    mUserMarker.remove();
}

另一种选择是在使用之前实例化mUserMarker

快乐的黑客攻击:)