我不明白爲什麼我不斷收到時,我使用的是asyntask來從一個API某些搜索結果,並填充一個arrayadapterAndroid的 - 從arrayadapter getCount將()AutoCompleteTextView NullPointerException異常
public class AddVenueActivity extends Activity implements TextWatcher{
private AutoCompleteTextView venue_auto_complete;
private ArrayList<String> venues = new ArrayList<String>();
private ArrayAdapter<SearchResult> adapter;
private SmartImageView location_image;
private VenueManager venueManager;
private Venue venue;
private Map<Integer, Object> current_venue_suggest_list = new HashMap<Integer, Object>();
private TextView venue_name_textview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addvenue);
try
{
venue_auto_complete = (AutoCompleteTextView) findViewById(R.id.add_venue_autocomplete);
ArrayList<SearchResult> search_results = new ArrayList<SearchResult>();
adapter = new ArrayAdapter<SearchResult>(this, android.R.layout.simple_list_item_1, search_results);
venue_auto_complete.setThreshold(2);
adapter.setNotifyOnChange(true);
venue_auto_complete.addTextChangedListener(this);
venue_auto_complete.setAdapter(adapter);
venue_name_textview = (TextView) findViewById(R.id.add_venue_venue_name);
venue_name_textview.setVisibility(View.GONE);
venueManager = new VenueManager();
location_image = (SmartImageView) findViewById(R.id.add_venue_location_image);
venue_auto_complete.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
venue_auto_complete.setDropDownHeight(LayoutParams.WRAP_CONTENT);
}
});
venue_auto_complete.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> listView, View arg1, int position, long arg3) {
try
{
SearchResult search_result = (SearchResult) listView.getAdapter().getItem(position);
Toast.makeText(AddVenueActivity.this, search_result.toString(), Toast.LENGTH_LONG).show();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(venue_auto_complete.getWindowToken(), 0);
venue_auto_complete.setText(search_result.venue_name);
venue_auto_complete.dismissDropDown();
venue_auto_complete.setDropDownHeight(0);
// // go find the venue we've selected
RequestParams params = new RequestParams();
params.put("id", search_result.id.toString());
Api.get("/venue", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject json_response)
{
try
{
venue = venueManager.addVenueFromJSON(json_response);
venue_name_textview.setVisibility(View.VISIBLE);
venue_name_textview.setText(venue.name);
}
catch(Exception e)
{
Log.v("Error", e.toString());
}
}
});
}
catch(Exception e)
{
Log.v("EROORS", e.toString());
}
}
});
}
catch(Exception e)
{
e.printStackTrace();
Log.v("ERROR", e.toString());
}
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// if(arg0.length() > current_query.length())
// {
// current_query = arg0.toString();
search(arg0.toString());
// }
}
public void search(String query)
{
try
{
RequestParams params = new RequestParams();
params.put("q", query);
Log.v("SEARCH", "Search query for " + query);
Api.get("/search", params, new JsonHttpResponseHandler()
{
@Override
public void onSuccess(JSONObject json_response)
{
try
{
if(adapter != null){
adapter.clear();
JSONArray venue_searchs = json_response.getJSONArray("response");
for(int i = 0; i < venue_searchs.length(); i++)
{
JSONObject venue_search_result = venue_searchs.getJSONObject(i);
if(venue_search_result.getString("address") != "null")
{
adapter.add(new SearchResult(venue_search_result.getInt("id"), venue_search_result.getString("name"), venue_search_result.getString("address")));
//adapter.add(venue_search_result.getString("name") + "\n" + venue_search_result.getString("address"));
}
else
{
adapter.add(new SearchResult(venue_search_result.getInt("id"), venue_search_result.getString("name"), ""));
//adapter.add(venue_search_result.getString("name"));
}
}
venue_auto_complete.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
catch(Exception e)
{
Log.v("ERROR", e.toString());
}
}
});
}
catch(Exception e)
{
Log.v("error", e.toString());
}
}
}
一個NullPointerException日誌cat輸出
03-11 14:50:55.334: E/AndroidRuntime(21313): FATAL EXCEPTION: main
03-11 14:50:55.334: E/AndroidRuntime(21313): java.lang.NullPointerException
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:291)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.widget.AdapterView.checkFocus(AdapterView.java:689)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:812)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:469)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.os.Looper.loop(Looper.java:150)
03-11 14:50:55.334: E/AndroidRuntime(21313): at android.app.ActivityThread.main(ActivityThread.java:4385)
03-11 14:50:55.334: E/AndroidRuntime(21313): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 14:50:55.334: E/AndroidRuntime(21313): at java.lang.reflect.Method.invoke(Method.java:507)
03-11 14:50:55.334: E/AndroidRuntime(21313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
03-11 14:50:55.334: E/AndroidRuntime(21313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-11 14:50:55.334: E/AndroidRuntime(21313): at dalvik.system.NativeStart.main(Native Method)
把你的日誌從日誌貓 – DjHacktorReborn 2013-03-11 14:56:46