调用另一个类后,getter返回null

时间:2018-09-28 16:29:06

标签: java android null getter-setter

我想使用getter和setter将标记范围移到另一个类。但是getter总是返回空值。这对我完全没有意义。我正在努力找出这里出了什么问题。

这是我的Location1课 我在哪里使用塞特犬

public class Location1 extends AppCompatActivity implements OnMapReadyCallback {

//do changes inside loaded map
@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(Location1.this, "Map Is Ready", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onMapReady: map is ready");
    mMap = googleMap;

    if (permission_Granted) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);
        mMap.getUiSettings().setCompassEnabled(true);



        //Afer Clicking google map marker info window
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {


                //Here using setters 
                marker1 marker1 = new marker1();
                marker1.setMarkerLat(marker.getPosition().latitude);

                Toast.makeText(Location1.this,String.valueOf(marker.getPosition().latitude),Toast.LENGTH_SHORT).show();

                startActivity(new Intent(Location1.this, pop.class));
            }
        });
}

}

和我的marker1类

public class marker1 {

Double markerLat;

public Double getMarkerLat() {
    return markerLat;
}

public void setMarkerLat(Double markerLat) {
    this.markerLat = markerLat;
}
}

和流行班 我在哪里使用返回空值的吸气剂

public class pop extends AppCompatActivity   {

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


    GarageNameTextView = (TextView) findViewById(R.id.nameView);
    GarageAddrTextView = (TextView) findViewById(R.id.addresView);
    GarageContactTextView = (TextView) findViewById(R.id.phoneView);

    dbGarages= FirebaseDatabase.getInstance().getReference("Garages");

    //Using getters
    marker1 marker1 = new marker1();
    String lat=String.valueOf(marker1.getMarkerLat());

    Toast.makeText(pop.this,lat, Toast.LENGTH_SHORT).show();

}
}

我被困在这里。无法前进..任何帮助将不胜感激.. 谢谢。

1 个答案:

答案 0 :(得分:0)

@Kasun_Chamara要通过第二种活动获得位置,您可以通过以下几种方式:

第一: 您必须将markerLat设置为静态字段。

像这样:

public class marker1 {

static Double markerLat;

public Double getMarkerLat() {
    return markerLat;
}

public void setMarkerLat(Double markerLat) {
    this.markerLat = markerLat;
}
}

秒: 使用putExtra发送locationparameter:

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {


                //Here using setters 
                marker1 marker1 = new marker1();
                marker1.setMarkerLat(marker.getPosition().latitude);

                Toast.makeText(Location1.this,String.valueOf(marker.getPosition().latitude),Toast.LENGTH_SHORT).show();

                Intent myintent = new Intent(Location1.this, pop.class);
                myintent.putExtra("Location",marker.getPosition().latitude);
                startActivity(myintent);
            }
        });

在第二个活动中:

getIntent().getIntExtra("Location",0);

第三:

根据@skandigraun,您可以发送其对象:

How to pass an object from one activity to another on Android