2011-03-31 173 views
0

嘿傢伙,即時通訊工作在一個簡單的測驗和即時消失的瘋狂! 問題是,一切工作(數據庫創建,因爲它應該),除非我試圖從數據庫中獲取字符串顯示java.lang.NullPointerException.I檢查uri更正和數組中的項目數!!我試圖找出爲什麼這會發生5個小時,我被困在這裏!!!我不知道該做什麼事情!!你的幫助不僅僅是讚賞!Android的定製內容提供商

在這裏我想獲得字符串我的主類是一個大膽

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

,並在我的清單我成功地添加提供程序和加載,因爲它應該! 爲什麼會發生這種錯誤?我可以看到任何錯誤!

+0

列不應該硬編碼(在我看來)。你能發佈錯誤信息嗎? – alibi 2011-03-31 21:54:34

回答

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) 
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); 
+0

謝謝你的超快回復!!我仍然嘗試,但仍然不工作:( – DarkPain 2011-03-31 20:05:56