Hey guys,im working on a simple quiz and im gone crazy !! The problem is that everything is working (database created as it shoulds) except when i am trying to get string from database shows java.lang.NullPointerException.I checked the uri is corrected and the number of items in array!!I am trying to find out why this is happening for 5 hours and i am stucked here!!!I dont know what elso to do!!Your help is more than appreciated!!
My main class where i am trying to get string is that one with bold
Uri newUri = ContentUris.withAppendedId(
QuestionsProvider.CONTENT_URI,
this.currentQuestion);
Log.d(TAG, "SHOWQUESTION " + " URI="+newUri.toString());
Cursor cursor = cr.query(newUri,
null, null, null, null);
if (cursor.moveToFirst()) {
**question.setText(cursor.getString(
QuestionsProvider.QUESTION_COLUMN)); //HERE I AM GETTING THE ERROR
currentAnswer = cursor.getString(
QuestionsProvider.ANSWER_COLUMN);**
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String text;
String answerGiven =
answer.getText().toString();
answer.setText("");
if (answerGiven.
equalsIgnoreCase(currentAnswer))
{text = "Correct";
}else{
text = "Wrong - "+currentAnswer;
Toast.makeText(getApplicationContext(),
text, Toast.LENGTH_SHORT).show();
开发者_如何转开发 }
}});
}
cursor.close();
dialog.show();
and in my manifest i add succesfully the provider and is loading as it should!! Why this error happens??I can see anything wrong!!
It doesn't look like you're specifying a projection in the query:
Cursor cursor = cr.query(newUri, null, null, null, null);
Try adding a projection with the columns you want returned:
Cursor cursor = cr.query(newUri, new String[] {KEY_ID, KEY_QUESTION, KEY_ANSWER}, null, null, null);
I found the solution guys! Thank you for helping me unstucking heh :P This is the solution i found!!
String columns[] = new String[] { QuestionsProvider.KEY_QUESTION, QuestionsProvider.KEY_ANSWER };
Uri mUri = QuestionsProvider.CONTENT_URI;
Cursor cur = managedQuery(mUri, columns, // Which columns to return
QuestionsProvider.KEY_ID+"="+currentQuestionNumber, // WHERE clause; which rows to return(all rows)
null, // WHERE clause selection arguments (none)
null // Order-by clause (ascending by name)
精彩评论