1

我有一个带有列表视图和 TextEdit 的活动,用于在该列表视图中进行搜索,当我开始该活动时,所有项目都已加载,但是当我尝试在 EditText 中键入任何单词时,会出现 NullPointerException。

    04-06 19:35:23.249: E/AndroidRuntime(8354): FATAL EXCEPTION: main
    04-06 19:35:23.249: E/AndroidRuntime(8354): java.lang.NullPointerException
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.widget.AdapterView.checkFocus(AdapterView.java:689)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.widget.AdapterView$AdapterDataSetObserver.onInvalidated(AdapterView.java:812)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.database.DataSetObservable.notifyInvalidated(DataSetObservable.java:43)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.widget.BaseAdapter.notifyDataSetInvalidated(BaseAdapter.java:54)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.widget.SimpleAdapter$SimpleFilter.publishResults(SimpleAdapter.java:383)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.os.Looper.loop(Looper.java:130)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at android.app.ActivityThread.main(ActivityThread.java:3768)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at java.lang.reflect.Method.invoke(Method.java:507)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
    04-06 19:35:23.249: E/AndroidRuntime(8354):     at dalvik.system.NativeStart.main(Native Method)

这是活动代码:

    package com.ferresis;

    import java.util.ArrayList;
    import java.util.HashMap;

    import android.app.ListActivity;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;

    import com.ferresis.db.DBAdapter;
    import com.ferresis.db.DBAdapter.LocalBinder;
    import com.ferresis.db.Productos.ProductosColumns;

    public class Act_Productos extends ListActivity {
        final String[] from = {ProductosColumns.CODIGO ,ProductosColumns.DESCRIPCION};
        final int[] to = new int[] {R.id.rowProdutos_Codigo, R.id.rowProdutos_Descripcion};

        DBAdapter dbAdapter;
        Boolean mBound;

        SimpleAdapter ListAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_productos);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.act__productos, menu);
            return true;
        }

        @Override
        protected void onStart() {
            super.onStart();
            // Enlazamos con el Servicio
            Intent intent = new Intent(this, DBAdapter.class);
            bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }

        @Override
        protected void onStop() {
            super.onStop();
            // Nos desenlazamos del servicio
            if (mBound) {
                unbindService(mConnection);
                mBound = false;
            }
        }

        private ServiceConnection mConnection = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName className, IBinder service) {
                // Enlazamos con LocalService, casteamos el IBinder y obtenemos así
                // la instancia del Servicio
                LocalBinder binder = (LocalBinder) service;
                dbAdapter = binder.getService();
                mBound = true;
                loadProductos();
            }

            @Override
            public void onServiceDisconnected(ComponentName arg0) {
                mBound = false;
            }
        };

        private void loadProductos() {
            ArrayList<Producto> productos = dbAdapter.getAllProductos();

            // Fill List with DATA
            ArrayList<HashMap<String, String>> HProductos = new ArrayList<HashMap<String, String>>();
            for (Producto producto : productos) {

                HashMap<String, String> Data = new HashMap<String, String>();

                Data.put(ProductosColumns._ID, String.valueOf(producto.getID()));
                Data.put(from[0], producto.getCodigo());
                Data.put(from[1], producto.getDescripcion());

                HProductos.add(Data);
            }

            ListAdapter = new SimpleAdapter(this, HProductos,
                    R.layout.row_productos, from, to);
            setListAdapter(ListAdapter);

            EditText etSearchbox=(EditText)findViewById(R.id.txtProducto_Busqueda);
            final ListView lv = getListView();
            lv.setTextFilterEnabled(true);

            etSearchbox.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub
                    Act_Productos.this.ListAdapter.getFilter().filter(arg0);
                    Log.d("Log","p1");
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub

                }

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

                }
            }); 

            lv.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
                    //String selectedFromList =(String) ();
                      Intent returnIntent = new Intent();
                      String item = "" + lv.getItemAtPosition(myItemInt);
                      item = item.substring(item.indexOf("=")+1,item.indexOf(","));
                      returnIntent.putExtra("idProducto",item);
                      setResult(RESULT_OK,returnIntent);
                      finish();                             
                  }                 
            });     
        }       

    }
4

0 回答 0