0

我上了这门课,但 onCreate 里面什么都没有发生?它不会在初始化时自动调用吗?

代码如下,非常感谢!- 迈克尔

package com.michealbach.livedrops;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;

public class AnyClass extends Activity {

double latitude = 0; 
double longitude = 0;

String addressString = "Address Initialized";
String postalString = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    postalString = "Why doesn't this String change happen at all?";

  }


public double getLongitude() {
    return longitude;
}

public double getLatitude() {
    return latitude;
}

public String getAddress() {
    return addressString;
}
public String getPostal() {
    return postalString;
}
}

这样做会导致“应用程序意外停止。请重试”。如果我将字符串初始化为一组字符而不是 null,它将保持这种状态。它不应该执行 onCreate() 内部的操作并进行更改吗?

4

1 回答 1

1

onLocationChanged() 只是你程序中的一个类方法,它没有连接到任何东西。你所要做的


private final LocationListener locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

             // Do stuff

            }


        @Override
        public void onProviderDisabled(String provider) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

    };


然后在你的 onCreate()


LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
cr = new Criteria();
cr.setPowerRequirement(Criteria.POWER_LOW);
cr.setAccuracy(Criteria.ACCURACY_FINE);
provider = lm.getBestProvider(cr,true);
lm.requestLocationUpdates(provider, minTIME, minDISTANCE, locationListener );


于 2010-08-23T21:53:21.387 回答