上下文NULL指针 [英] Context null Pointer

查看:113
本文介绍了上下文NULL指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注这个教程:的http://开发商。 android.com/resources/tutorials/views/hello-mapview.html
但在中的onTap mContext抛出NullPointerException异常..任何人都知道为什么吗?这里是我的code ..

I've been following this tutorial: http://developer.android.com/resources/tutorials/views/hello-mapview.html but in onTap mContext is throwing a NullPointerException.. anyone know why? Here's my code..

    public class Mapitems extends ItemizedOverlay{
Context mContext;

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

public Mapitems(Drawable defaultMarker) {
      super(boundCenterBottom(defaultMarker));
    }

public Mapitems(Drawable defaultMarker, Context context) {
      super(defaultMarker);
      mContext = context;
    }
@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

@Override
public int size() {
    return mOverlays.size(); 
}

  }

//编辑:我仍然有这个问题。赏金是任何人谁可以给我一个解释,为什么我得到这种错误,我会如何纠正呢?

//edit: I'm still having problems with this. Bounty is for anyone who can give me an explanation as to why I'm getting this sort of error and how would I correct it?

// EDIT2:好像过去的回答让我点击这个项目,但犯规展示其在图形页面图标..任何人都知道为什么?

//edit2: It seems past answer allows me to click the item but doesnt show its icon in the mapview.. anyone know why??

推荐答案

看你的code,你可能调用构造简单

Looking at your code, you probably call the simple constructor

public Mapitems(Drawable defaultMarker)

此构造函数不设置 mContext ,这就是为什么你会得到一个NullPointerException异常。结果
添加像 mContext =新的上下文行() mContext = android.content.getApplicationContext()可能会解决问题

This constructor does not set the mContext and that's why you get a NullPointerException.
Adding a line like mContext = new Context() or mContext = android.content.getApplicationContext() might solve the problem.

另外,也可以是一个空的参数被提供给其他的构造

It is also possible that a null argument is supplied to the other constructor

public Mapitems(Drawable defaultMarker, Context context)

插入指定mContext并在必要时提供一个默认的情况下则可以解决该问题时,空检查。

Inserting a null check when assigning mContext and if necessary providing a default context may then solve the problem.

的构造是这样的:

public Mapitems(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    mContext = android.content.getApplicationContext();
    // or: mContext = new Context();
}

public Mapitems(Drawable defaultMarker, Context context) {
    super(defaultMarker);
    if(context==null)
        mContext = android.content.getApplicationContext();
        // or: mContext = new Context();
    mContext = context;
}

希望这会解决您的问题。

Hope this solves your problem.

这篇关于上下文NULL指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆