我是Android新手。调用getContentResolver()
函数时,我总是得到NullPointerException
。我不知道为什么。代码如下:
@EActivity(R.layout.activity_job_details)
@OptionsMenu(R.menu.main)
public class JobDetailsActivity extends ActionBarActivity {
@Extra
Job jobObject;
@ViewById
ListView lwPickups;
PickupAdapter adapter;
@AfterViews
public void afterViews() {
initActionBar();
adapter = new PickupAdapter(JobDetailsActivity.this,
R.layout.row_pickup, R.id.twPickup, jobObject.getPickups());
lwPickups.setAdapter(adapter);
}
private void initActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(jobObject.getDesc());
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
然后我尝试在同一个类中运行查询事件:
boolean result=queryEvent("123",JobDetailsActivity.this);
@SuppressLint("NewApi")
public boolean queryEvent(String event_id,Context context) {
String[] projection= new String[]{Events.TITLE};
ContentResolver contentResolver=context.getContentResolver(); // NullPointerException
Cursor calCursor=contentResolver.query(Events.CONTENT_URI, projection, Events._ID+"="+event_id, null, null);
if(calCursor.moveToFirst()){
do{
String id = calCursor.getString(0);
if(id==event_id){
return true;
}
}while(calCursor.moveToNext());
}
return false;
}
}
有人可以提供帮助吗?是否导致在创建活动之前无法访问getContentResolver()?如果是这样,在这种情况下,我应该在哪里放置getContentResolver(),因为我使用@EActivity(R.layout.activity_job_details)?感谢