我是Android新手。我想在onLocationChanged
方法中顯示Toast或Snackbar,但是我在此方法中獲取了NullPointerException
。我該如何解決這個問題?在上下文的onLocationChanged方法中獲取空指針
private void displayTost(Location location)
{
Toast.makeText(getBaseContext(),"Location changed......",Toast.LENGTH_LONG).show();
}
日誌輸出:
java.lang.NullPointerException
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.content.ComponentName.<init>(ComponentName.java:77)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.content.Intent.<init>(Intent.java:4044)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at com.vaidya.prasad.bumpd.activities.BumpdActivity.displayTost(BumpdActivity.java:323)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at com.vaidya.prasad.bumpd.activities.BumpdActivity.onLocationChanged(BumpdActivity.java:282)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:110)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
11-19 12:43:35.944 23614-23614/com.vaidya.prasad.bumpd E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
第一次正確發佈您的代碼和logcat。 –