如何从类到主要活动获取变量?

时间:2018-01-11 15:02:32

标签: java android variables

在我的Android应用程序(Wheather应用程序)中,我有一个主要活动(在屏幕上显示wheater)和一个类(获取手机的当前位置)。 "位置" class获取经度和经度,我想在我的主要活动中发送它以使用它们。为此,我尝试使用getter,但这似乎不起作用。以下是这两个类的代码:

位置等级:(最后注意吸气剂)

public class Position extends AppCompatActivity implements LocationListener {

private double longitude;
private double latitude;
private LocationManager locationManager;

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

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

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

        return;
    }
    Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

    onLocationChanged(location);


}

@Override
public void onLocationChanged(Location location) {
    longitude=location.getLongitude();
    latitude=location.getLatitude();

}

public double getLongitude1() {
    return this.longitude;
}

public double getLatitude1() {
    return this.latitude;
}

Main_Activity :(再次注意我尝试使用纬度和经度的最后四行)

public class MainActivity extends AppCompatActivity {

TextView cityField, detailsField, currentTemperatureField, humidity_field, pressure_field, weatherIcon, updatedField;

Typeface weatherFont;

Position position = new Position();

private double latitude1;
private double longitude1;
private String latitude2;
private String longitude2;

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


    weatherFont = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/weathericons-regular-webfont.ttf");

    cityField = (TextView)findViewById(R.id.city_field);
    updatedField = (TextView)findViewById(R.id.updated_field);
    detailsField = (TextView)findViewById(R.id.details_field);
    currentTemperatureField = (TextView)findViewById(R.id.current_temperature_field);
    humidity_field = (TextView)findViewById(R.id.humidity_field);
    pressure_field = (TextView)findViewById(R.id.pressure_field);
    weatherIcon = (TextView)findViewById(R.id.weather_icon);
    weatherIcon.setTypeface(weatherFont);


    Function.placeIdTask asyncTask =new Function.placeIdTask(new Function.AsyncResponse() {
        public void processFinish(String weather_city, String weather_description, String weather_temperature, String weather_humidity, String weather_pressure, String weather_updatedOn, String weather_iconText, String sun_rise) {

            cityField.setText(weather_city);
            updatedField.setText(weather_updatedOn);
            detailsField.setText(weather_description);
            currentTemperatureField.setText(weather_temperature);
            humidity_field.setText("Humidity: "+weather_humidity);
            pressure_field.setText("Pressure: "+weather_pressure);
            weatherIcon.setText(Html.fromHtml(weather_iconText));

        }
    });
    latitude1 = position.getLatitude1();
    longitude1 = position.getLongitude1();
    latitude2 = String.valueOf(latitude1);
    longitude2 = String.valueOf(longitude1);
    asyncTask.execute(latitude2, longitude2); //  asyncTask.execute("Latitude", "Longitude")

}

为什么我的android监视器中总是得到latitude2 = 0.0和longitude2 = 0.0?

5 个答案:

答案 0 :(得分:0)

您有两种不同的活动。不是活动和后台服务。只有一个UI线程可以运行活动。因此,当MainActivity正在运行时,Position活动位于后台并且未运行。而且您无法使用Position position = new Position();创建活动对象。

答案 1 :(得分:0)

为什么您的Position班级为Activity?除非您将该类作为onCreate启动,否则永远不会调用Activity方法。从中移除AppCompatActivity并以单独的方式移动onCreate方法,例如getLocation

您还希望将Context传递给Position课程。为该

创建一个构造函数
public Position(Context context) {
    this.context = context;
}

并将其用于系统调用。

答案 2 :(得分:0)

无法共享私有变量。将其更改为。

public double longitude1;
public double latitude1;

答案 3 :(得分:0)

您实际上不需要Position从活动扩展。我可以了解您要执行的操作,只是想从LocationManager获取位置,并将结果发送到MainActivity。如果您只是在LocationManager中创建MainActivity个实例并将该位置的结果传递到MainActivity内的任何内容,那么应该没问题。

public class MainActivity extends AppCompatActivity {

TextView cityField, detailsField, currentTemperatureField, humidity_field, pressure_field, weatherIcon, updatedField;

Typeface weatherFont;

Position position = new Position();

private double latitude1;
private double longitude1;
private String latitude2;
private String longitude2;

private LocationManager mLocationManager;

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


    weatherFont = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/weathericons-regular-webfont.ttf");

    cityField = (TextView)findViewById(R.id.city_field);
    updatedField = (TextView)findViewById(R.id.updated_field);
    detailsField = (TextView)findViewById(R.id.details_field);
    currentTemperatureField = (TextView)findViewById(R.id.current_temperature_field);
    humidity_field = (TextView)findViewById(R.id.humidity_field);
    pressure_field = (TextView)findViewById(R.id.pressure_field);
    weatherIcon = (TextView)findViewById(R.id.weather_icon);
    weatherIcon.setTypeface(weatherFont);


    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // do check permission staff as you post before

    Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

    // do what you want with the location now.

基本上我认为你不必进行Position课程。您可以直接获取位置,然后使用它。

答案 4 :(得分:0)

我建议您在代码中添加以下改进。

  1. 您需要在MainActivity的onCreate()方法中创建Position类的对象。由于onCreate()在其他所有内容之前运行,因此必须在此方法中定义Position类。

  2. 将经度和纬度的变量设为公开,以便在其他类中访问它们。

  3. 职位类不需要扩展AppCompatActivity。您可以使用构造函数并在那里定义所有内容,而不是使用此方法和OnCreate()方法。