Android:没有任何过去onCreate正在发生

时间:2010-08-23 21:25:03

标签: android location geocoding locationmanager oncreate

我上了这堂课,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()中的内容并进行更改吗?

1 个答案:

答案 0 :(得分: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 );