2016-07-11 54 views
-1

我試圖在if語句中調用TextView中的「getText」時出現此錯誤。奇怪的是,當我在if語句之前的Log語句的相同視圖中調用相同的方法時,沒有錯誤彈出。我看過很多其他類似的問題,並且我嘗試清理並重建我的版本無濟於事。當我嘗試運行它時,出現的錯誤是錯誤的:布爾值不能被解除引用。 TextView不是原始類型,所以我相信我應該可以調用它的方法。我有下面的方法,請告訴我是否需要其他代碼。無法解析TextView上的方法'getText()'/ error:布爾值不能被解除引用?

private void doSearch(Cursor query) { 
     // get a Cursor, prepare the ListAdapter 
     // and set it 
     Cursor c = query; 
     startManagingCursor(c); 


     String[] from = new String[] {"QUANTITY", "_id"}; 
     int[] to = new int[] {android.R.id.text1}; 
     SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, from, to); 
     mListView.setAdapter(cursorAdapter); 
     Log.e("doSearch method:", "has been called"); 

     mListView.setOnItemClickListener(
       new AdapterView.OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
         // When clicked, log with the TextView text 
         Log.e("doSearch method:", "Answer: " + ((TextView) view).getText()); //no error here 

         if(cMap.containsKey((TextView) view).getText()){ //error here 
          //start new activity 
         } else if (chMap.containsKey((TextView) view).getText()){//error here 
          //start new activity 
         } else if (aMap.containsKey((TextView) view).getText()){//error here 
          //start new activity 
         } 


        } 
       }); 
    } 

非常感謝您的幫助。

+0

containsKey()返回布爾值而不是textview。因此錯誤。你可能想獲取(),而不是(警告檢查爲空) –

+0

我不想要一個布爾值在if語句中使用嗎? – grassss

+0

一個布爾沒有getText方法.. –

回答

2

你傳入視圖作爲參數傳遞給containsKey方法:

cMap.containsKey((TextView) view).getText() 

應該

cMap.containsKey(((TextView) view).getText()) 

您得到基本的錯誤說cMap.containsKey(圖).getText()是不是布爾值

+0

注意:containsKey接受一個對象,所以傳遞一個視圖到它可能是好 –

+0

是的,但數據邏輯不存在。我認爲她試圖將textview文本用作關鍵字,但錯過了一個括號,而不是將視圖用作關鍵字。它只是更有意義 – SoroushA

+0

是的,我添加了評論,因爲您的答案開始於「**因爲**您將View作爲參數傳遞給containsKey」 –