本文介绍了ArrayAdapter.getCount()NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序下面code:

I have following code in my android application:

   protected ArrayList<String> message;

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.where_to_go);

      whereToGoField = (AutoCompleteTextView)findViewById(R.id.WhereToGoField);

      adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
      whereToGoField.addTextChangedListener(whereToGoFieldInputWatcher);
      whereToGoField.setAdapter(adapter);
   }


   TextWatcher whereToGoFieldInputWatcher = new TextWatcher() {
      public void onTextChanged(final CharSequence s, int start, int before, int count) {
         Thread t = new Thread() {
            public void run() {
               Object o = Server.getStreetSuggestions(s.toString());
               if (o!=null) {
                  message = (ArrayList<String>)o;
               }
               messageHandler.sendEmptyMessage(0);
            }
         };
         t.start();
      }

   };

   private Handler messageHandler = new Handler() {
      public void handleMessage(Message msg) {
         adapter.clear();
          for(int i=0; i<message.size(); ++i){
              adapter.add(message.get(i));
          }
          adapter.notifyDataSetChanged();
      }
   };

Basicaly它会从我的服务器响应,并填充autocompletetextview此响应。

Basicaly it gets response from my server and populate this response in autocompletetextview.

但有些时候,我收到以下错误:

But some times I getting following error:

04-25 16:08:42.288: ERROR/AndroidRuntime(6193): FATAL EXCEPTION: main
04-25 16:08:42.288: ERROR/AndroidRuntime(6193): java.lang.NullPointerException
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:322)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.widget.AdapterView.checkFocus(AdapterView.java:720)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:844)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:74)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:85)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.widget.ArrayAdapter$ArrayFilter.publishResults(ArrayAdapter.java:500)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:313)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.os.Handler.dispatchMessage(Handler.java:130)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.os.Looper.loop(Looper.java:154)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at android.app.ActivityThread.main(ActivityThread.java:4668)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at java.lang.reflect.Method.invoke(Method.java:552)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
04-25 16:08:42.288: ERROR/AndroidRuntime(6193):     at dalvik.system.NativeStart.main(Native Method)

我不明白它是从哪里来的。

I can't understand where it came from.

你能帮帮我吗?

推荐答案

这是我做了什么:

   TextWatcher whereToGoFieldInputWatcher = new TextWatcher() {
      public void onTextChanged(final CharSequence s, int start, int before, int count) {
         new AsyncTask<Integer, Void, ArrayList<String>> () {

            @Override
            protected synchronized void onPostExecute(ArrayList<String> result) {
                //make arrayAdapter from result
                //set adapter to AutoCompleteTextView
                adapter.clear();
                for (String name : result) {
                   adapter.add(name);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            protected ArrayList<String> doInBackground(Integer... params) {
               // make connection to remote server
               // retrive response from remote server
               // make arrayList from response
               return Server.getSuggestions(s.toString());
            }
        }.execute(1);
      }

      @Override
      public void afterTextChanged(Editable arg0) {
         // TODO Auto-generated method stub

      }

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
         // TODO Auto-generated method stub

      }
   };

和看起来像它的工作原理。

And looks like it works.

这篇关于ArrayAdapter.getCount()NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:44