我試圖在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
}
}
});
}
非常感謝您的幫助。
containsKey()返回布爾值而不是textview。因此錯誤。你可能想獲取(),而不是(警告檢查爲空) –
我不想要一個布爾值在if語句中使用嗎? – grassss
一個布爾沒有getText方法.. –