在onLocationChanged方法中为上下文获取空指针

时间:2015-11-19 06:53:41

标签: android nullpointerexception

我是Android新手。我想在onLocationChanged方法中显示Toast或Snackbar,但我在此方法中为上下文获取NullPointerException。我该如何解决这个问题?

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)

5 个答案:

答案 0 :(得分:1)

您也可以尝试使用监听器的类名extends Activity implements LocationListener,从您的logcat我可以猜到活动名称是BumpdActivity,所以为什么不尝试下面的行..

Toast.makeText(BumpdActivity.this,"Location changed......",Toast.LENGTH_LONG).show();

如果这不起作用请分享你的课程..

答案 1 :(得分:0)

试试这个,

private void displayTost() {
    Toast.makeText(getBaseContext(), "Location changed......", Toast.LENGTH_LONG).show();
}

PS:在上述方法中发布使用Location location的相关代码。

答案 2 :(得分:0)

试试这个

Toast.makeText(getApplicationContext(),"Location changed......",Toast.LENGTH_LONG).show();

getApplicationContext() 返回当前进程的单个全局Application对象的上下文。

答案 3 :(得分:0)

在上下文中输入您的类名称dot或传递这样的上下文参数。

private void displayTost(Context context, Location location)
{
    Toast.makeText(context,"Location changed......",Toast.LENGTH_LONG).show();
}

答案 4 :(得分:0)

如果你试图在一个没有像在recycler视图中的适配器类那样扩展活动或appcompactactivity的类中获取Context(),那么只需编写一个参数化的构造函数,它接受Context作为扩展activity的类的参数。&amp;然后通过创建它的对象将上下文传递给适配器类。 如下面的代码public RVAdapter(Context contexts, List<quiz>quizList) { this.mContext = contexts; this.quizList=quizList; } & in activity just pass context as RVAdapter adapter=new RVAdapter(this,qw);