2013-07-19 70 views
3

我有一個ArrayList,其中有60-70個元素。我如下所示將一個適配器設置爲AutoCompleteTextView。AutoCompleteTextView不顯示下拉框,出現警告

AutoCompleteTextView mRecipient = (AutoCompleteTextView) this.findViewById(R.id.recipient); 
     mRecipient.setThreshold(1); 
     Log.i("Array list", ""+names);// Here I got the arrayList 
     ArrayAdapter<String> nameadapter=new ArrayAdapter<String>(MyClass.this, android.R.layout.simple_list_item_1, names); 
     mRecipient.setAdapter(nameadapter); 

但下拉列表顯示不出來,我在logcat中得到了一些警告(不errror)。

04-21 17:15:53.017: W/Filter(15093): An exception occured during performFiltering()! 
04-21 17:15:53.017: W/Filter(15093): java.lang.NullPointerException 
04-21 17:15:53.017: W/Filter(15093): at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:437) 
04-21 17:15:53.017: W/Filter(15093): at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234) 
04-21 17:15:53.017: W/Filter(15093): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-21 17:15:53.017: W/Filter(15093): at android.os.Looper.loop(Looper.java:123) 
04-21 17:15:53.017: W/Filter(15093): at android.os.HandlerThread.run(HandlerThread.java:60) 

此外,我有一個ListView下面這個,現在是空的。

有人可以弄清楚這個警告的含義嗎?

+1

確保'names'不爲空 –

+1

實際上,請確保'names'中的項目都不爲'null'。 – Vikram

+0

'names'不爲null,但列表中有空項目。這是問題嗎? – Nizam

回答

4

警告來自於下面的代碼片段在ArrayAdapter.performFiltering(CharSequence)

String prefixString = prefix.toString().toLowerCase(); // entered by user 
final ArrayList<T> values = mOriginalValues; 
final int count = values.size(); 
final ArrayList<T> newValues = new ArrayList<T>(count); // this will be returned 
for (int i = 0; i < count; i++) { 
    final T value = values.get(i);      // in your case, 'value' is null at times 
    final String valueText = value.toString().toLowerCase(); // the warning 

    // First match against the whole, non-splitted value 
    if (valueText.startsWith(prefixString)) { 
     newValues.add(value); 
    } else { 
     final String[] words = valueText.split(" "); 
     final int wordCount = words.length; 
     for (int k = 0; k < wordCount; k++) { 
      if (words[k].startsWith(prefixString)) { 
       newValues.add(value); 
       break; 
      } 
     } 
    } 
} 

所以,在names檢查null值。