2010-08-28 88 views
1

我遇到了來自教程代碼的幾個問題:ListView Tutorial。該代碼snipets給我的問題是:你好ListView教程問題

static final String[] COUNTRIES = new String[] { 

Toast.makeText(getApplicationContext(), ((View) view).getText(), 

的錯誤閱讀下列分別:

  • 的參數國家是非法的改性劑;只有最終被允許
  • 方法的getText()是未定義的類型查看

我真的不知道該怎麼做來解決這些問題。我對Java和Android編程非常陌生,請詳細解釋它。我會把整個項目放在這裏,但我不太清楚如何打包並放在這裏。 (就像我說的,我是一個新手)。提前致謝。

package com.android.hellolistview; 


import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class HelloListView extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 



     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, TextView view, 
      int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
       Toast.LENGTH_SHORT).show(); 
     } 
     }); 
    } 

private void setListAdapter(ArrayAdapter<String> arrayAdapter) { 
    // TODO Auto-generated method stub 

} 
    static final String[] COUNTRIES = new String[] { 
     "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", 
     "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", 
     "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", 
     "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", 
     "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", 
     "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", 
     "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", 
     "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", 
     "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", 
     "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", 
     "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", 
     "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", 
     "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", 
     "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland", 
     "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", 
     "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", 
     "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", 
     "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary", 
     "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", 
     "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", 
     "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", 
     "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", 
     "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", 
     "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", 
     "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", 
     "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", 
     "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", 
     "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", 
     "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena", 
     "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", 
     "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal", 
     "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", 
     "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea", 
     "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", 
     "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", 
     "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", 
     "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda", 
     "Ukraine", "United Arab Emirates", "United Kingdom", 
     "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", 
     "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara", 
     "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" 
     };} 
+0

你在哪裏放置COUNTRIES字符串數組? – 2010-08-28 00:55:37

回答

0
Toast.makeText(getApplicationContext(), ((View) view).getText() 

應該

Toast.makeText(getApplicationContext(), ((TextView) view).getText() 

View類沒有實現的getText方法,而TextView的擴展視圖,並實現了gettext方法。在你的代碼中,你將視圖轉換爲視圖,這實際上並沒有做任何事情。它需要被轉換爲TextView才能正常工作。

我在想你可能會把你的COUNTRIES字符串數組也放在錯誤的地方。

如果仍無法修復,請將所有HelloListView代碼粘貼到您的問題中。

此外,這可能是你一個良好的閱讀:http://developer.android.com/guide/topics/ui/index.html

+0

非常感謝。我移動了下面我稱之爲的國家,而且沒有任何錯誤。但是,當我將它加載到模擬器中並運行應用程序時,它是一個空白屏幕。你怎麼看?我將代碼添加到問題中。 – 2010-08-28 04:47:04

0

觀點不是方法的getText(),TextView的有方法的getText(),TextView的擴展視圖, 所以你的代碼必須想:

Toast.makeText(getApplicationContext(), ((TextView) view).getText()); 

您必須閱讀android SDK的api。

0

該數組的代碼應該在onCreate方法之後。

1

我也有一個模擬器的問題。我得到了「應用程序意外停止...」的消息。

我有同樣的行你:

public class HelloListView extends ListActivity { 

在我HelloListView清單,我有以下行:

<activity android:name=".HelloListViewActivity" 

當我把它改爲:

<activity android:name=".HelloListView" 

的應用程序開始在模擬器中工作。將該類重命名爲HelloListViewActivity也可能執行相同的技巧。還沒有嘗試過...

0

改變這樣的:公共類HelloListView延伸活動{ 到:公共類HelloListView擴展ListActivity { 並添加: 查看源代碼 打印?

1 import android.widget.ArrayAdapter; 
2 import android.app.ListActivity;