ContextWrapper中的空指针异常

时间:2010-12-31 11:34:27

标签: android

我正在使用ListActivity,这会抛出Null Pointer Exception

private ContextWrapper ctx;
return ctx.getContentResolver().query(
                Data.CONTENT_URI,
                new String[] {
                    Data.DATA1
                },
                Data.RAW_CONTACT_ID + "=" + this.getId() + " AND " + Data.MIMETYPE + "= '" + mimetype
                        + "'", null, null);

ctx未初始化,应该初始化为什么?

1 个答案:

答案 0 :(得分:0)

您的ListActivity是一个ContextWrapper(请参阅http://developer.android.com/reference/android/app/ListActivity.html处的Javadoc),因此不需要实例化一个。相反,只需在ListActivity本身上调用getContentResolver():

public class MyActivity extends ListActivity {
    ...
    return getContentResolver().query(
            Data.CONTENT_URI,
            new String[] {
                Data.DATA1
            },
            Data.RAW_CONTACT_ID + "=" + this.getId() + " AND " + Data.MIMETYPE + "= '" + mimetype
                    + "'", null, null);