我对Android编程很新,并一直在寻找解决这个问题的一些指示,但我似乎无法解决确切的问题。
我想我只需要一些指针,为什么以及它抛出NullPointerException的位置,以便我将来可以自己跟踪它。
相关代码的一些部分:
private static final long TWO_MINUTES = 1000 * 60 * 2;
private static final long TEN_SECONDS = 10000;
private static final float TEN_METERS = 10;
public static final String LOG_TAG = "Locally";
private static LocationManager locMan;
Button StopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(), "Attempting to start service",
Toast.LENGTH_SHORT);
startService(new Intent(LocallyActivity.this, LocallyService.class));
StopButton = (Button) findViewById(R.id.button1);
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
double lat = 0;
double longi = 0;
TextView t = (TextView) findViewById(R.id.textView1);
Criteria crit = new Criteria();
String strLoc = locMan.getBestProvider(crit, true);
Location lastKnown = locMan.getLastKnownLocation(strLoc);
if (lastKnown != null) {
lat = lastKnown.getLatitude();
longi = lastKnown.getLongitude();
long tsLong = System.currentTimeMillis() / 1000;
long lastTime = lastKnown.getTime();
long difference = tsLong - lastTime;
Time time = new Time();
time.set(Long.valueOf(lastTime));
t.setText("" + lat + ", " + longi + " (" + strLoc + ", time: "
+ time.hour + " " + time.minute + ")");
Object networkLocation = requestUpdatesFromProvider(strLoc, 32);
} else {
t.setText("No last location from " + strLoc);
}
}
private Location requestUpdatesFromProvider(final String provider,
final int errorResId) {
Location location = null;
Log.v(LOG_TAG, provider);
if (locMan.isProviderEnabled(provider)) {
locMan.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS,
listener);
location = locMan.getLastKnownLocation(provider);
} else {
Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show();
}
return location;
}
错误跟踪:
07-30 12:41:12.644: E/AndroidRuntime(20697): FATAL EXCEPTION: main
07-30 12:41:12.644: E/AndroidRuntime(20697): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.somnu.locally/com.somnu.locally.LocallyActivity}: java.lang.NullPointerException
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.os.Looper.loop(Looper.java:137)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-30 12:41:12.644: E/AndroidRuntime(20697): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:41:12.644: E/AndroidRuntime(20697): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 12:41:12.644: E/AndroidRuntime(20697): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-30 12:41:12.644: E/AndroidRuntime(20697): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-30 12:41:12.644: E/AndroidRuntime(20697): at dalvik.system.NativeStart.main(Native Method)
07-30 12:41:12.644: E/AndroidRuntime(20697): Caused by: java.lang.NullPointerException
07-30 12:41:12.644: E/AndroidRuntime(20697): at com.somnu.locally.LocallyActivity.requestUpdatesFromProvider(LocallyActivity.java:67)
07-30 12:41:12.644: E/AndroidRuntime(20697): at com.somnu.locally.LocallyActivity.onCreate(LocallyActivity.java:57)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.Activity.performCreate(Activity.java:4465)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-30 12:41:12.644: E/AndroidRuntime(20697): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-30 12:41:12.644: E/AndroidRuntime(20697): ... 11 more
第67行是:
if (locMan.isProviderEnabled(provider)) {
答案 0 :(得分:5)
我认为locMan is null so NPE...
使用
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
而不是
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);