Android:onCreate 之后的任何事情都没有发生

发布于 2024-09-15 20:14:28 字数 906 浏览 5 评论 0原文

我得到了这个课程,但 onCreate 内没有任何反应?初始化时不会自动调用吗?

代码如下,非常感谢! - Micheal

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() 内部的操作并进行更改吗?

I got this class, and nothing inside the onCreate is happening? Does it not automatically get called upon initialization?

Code is below, Thanks alot! - Micheal

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;
}
}

Doing this results in a "application has stopped unexpectedly. Please try again". If I initialize the string to some set of characters instead of null, it'll just stay that way. Should it not do what is inside the onCreate() and make that change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2024-09-22 20:14:28

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 );


onLocationChanged() is just a class method in your program, it's not conected to anything. You have to do


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) {

        }

    };


And then in your 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 );


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文