Android的自定义内容提供程序

时间:2011-03-31 19:39:49

标签: android database android-contentprovider

嘿伙计们,我正在进行一个简单的测验,我疯了! 问题是一切正常(数据库创建它应该)除非我试图从数据库中获取字符串显示java.lang.NullPointerException.I检查uri已更正和数组中的项目数!!我正在尝试找出为什么这发生了5个小时,我被困在这里!!!我不知道elso要做什么!!你的帮助不仅仅是赞赏!!

我想要获取字符串的主要类是带有粗体的

        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();

并在我的清单中,我成功地添加了提供程序并正在加载它应该! 为什么会出现这种错误?我看错了!

2 个答案:

答案 0 :(得分:0)

看起来您没有在查询中指定投影:

Cursor cursor = cr.query(newUri, null, null, null, null);

尝试添加要返回的列的投影:

Cursor cursor = cr.query(newUri, new String[] {KEY_ID, KEY_QUESTION, KEY_ANSWER}, null, null, null);

答案 1 :(得分:0)

我找到了解决方案的人!谢谢你帮助我解开嘘:P 这是我找到的解决方案!!

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)