简体   繁体   English

Android的自定义内容提供程序

[英]Custom Content Provider for android

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!! 问题是,除了我试图从数据库中获取字符串显示java.lang.NullPointerException之外,其他所有东西都在正常工作(数据库应该创建)。我检查了uri是否已更正并且数组中的项目数!!找出为什么这会持续5个小时,而我却被困在这里!!我不知道该怎么办!

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!! 谢谢您帮助我解开嘿: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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM