具有AutoCompleteTextView的SimpleCursorAdaptor在HTC设备上的onItemClick上提供NullPointerException

时间:2011-11-16 08:11:39

标签: android nullpointerexception simplecursoradapter onitemclicklistener

我遇到一个问题,我已尝试过几种不同的失败解决方案。

我有一个AutoCompleteTextView,其SimpleCursorAdaptor绑定到我的数据库以提取产品名称。当用户搜索产品时,名称显示正常。当他们点击产品时,会出现NullPointerException并导致应用程序崩溃。有趣的是,它只发生在HTC Devices上。我的三星工作,我的好友摩托罗拉工作,模拟器工作。只是不是HTC。

以下是来自Android Market提交的用户的堆栈跟踪。

java.lang.NullPointerException
at enders.pos.test.PointOfSale$8.onItemClick(PointOfSale.java:571)
at android.widget.AutoCompleteTextView.onCommitCompletion(AutoCompleteTextView.java:921)
at com.android.internal.widget.EditableInputConnection.commitCompletion(EditableInputConnection.java:78)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:309)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:75)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)

第571行是:

Cursor c = shop.getProdByName(text.getText().toString());

我相信TextView文本返回null。但我不确定为什么。

以下是PointOfsale类的一部分:

final int[] to = new int[] { android.R.id.text1 };
final String[] from = new String[] { "name" };

SimpleCursorAdapter Autoadapter =
        new SimpleCursorAdapter(this,
                android.R.layout.simple_dropdown_item_1line, null,
                from, to);

textView = (AutoCompleteTextView) findViewById(R.id.autoproduct);       
textView.setAdapter(Autoadapter);
textView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> listView, View view, int position, long id) {

        TextView text = (TextView) view;

        Cursor c = shop.getProdByName(text.getText().toString());

        if(c != null){
            if(c.getColumnIndex("_id") >= 0){
                Product product = new Product();

                product.setId(c.getInt(c.getColumnIndex("_id")));
                product.setBarcode(c.getString(c.getColumnIndex("barcode")));
                product.setName(c.getString(c.getColumnIndex("name")));
                product.setDesc(c.getString(c.getColumnIndex("desc")));
                product.setPrice(c.getFloat(c.getColumnIndex("price")));
                product.setCat(c.getInt(c.getColumnIndex("catid")));

                cart.AddProduct(product);
                c.close();
            }else{
                alertbox("Not Found", "Product not found");
            }
        }else{
            alertbox("Not Found", "Product not found");
        }

        textView.setText("");
        ((ProductAdapter) inventoryList.getAdapter()).notifyDataSetChanged();
    }
});

Autoadapter.setCursorToStringConverter(new CursorToStringConverter() {
    public String convertToString(android.database.Cursor cursor) {
        // Get the label for this row out of the "state" column
        final int columnIndex = cursor.getColumnIndexOrThrow("name");
        final String str = cursor.getString(columnIndex);
        return str;
    }
});

// Set the FilterQueryProvider, to run queries for choices
// that match the specified input.
Autoadapter.setFilterQueryProvider(new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
        // Search for states whose names begin with the specified letters.
        Cursor cursor = ProductDatabase.helper.fetchItemsByName(
                (constraint != null ? constraint.toString() : null));
        return cursor;
    }
});

2 个答案:

答案 0 :(得分:0)

在您收到错误的代码中添加一个日志,看看它是否从HTC设备的Logcat返回一个值。处理该设备的文本视图可能存在差异。请在某个系统中检查它们是否为空。

System.out.println(text,text.getText().toString());

如果它只为HTC设备返回一个null,那么你需要找到一个解决方法,因为你的视图层次结构逻辑是错误的(这里似乎没问题)或HTC设备有一个错误。如果是这样的话很难解决然后好运。

答案 1 :(得分:0)

我找到了解决问题的好方法!我已经摆脱了SimpleCursorAdaptor并实现了我自己的方法。我还确定了几个设备以不同的方式处理UI。所以我实现了一种强力方法来从4种不同的方法中获取数据。如果一个失败,它将继续下一个。但是一种方法应该始终获取数据。这是代码:

    Autoadapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line);
    Autoadapter.setNotifyOnChange(true);

    textView = (AutoCompleteTextView) findViewById(R.id.autoproduct);       
    textView.setAdapter(Autoadapter);
    textView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {

            boolean isAGo = false;
            String item = null;
            Cursor c = null;

            if(listView != null){
                item = listView.getItemAtPosition(position).toString();
                c = shop.getProdByName(item);
                isAGo = true;
            }

            if(isAGo == false){
                if(view != null){
                    TextView text = (TextView) view;
                    c = shop.getProdByName(text.getText().toString());
                    isAGo = true;
                }
            }

            if(isAGo == false){
                if(textView.getText() != null){
                    item = textView.getText().toString();
                    c = shop.getProdByName(item);
                    isAGo = true;
                }
            }

            if(isAGo == false){
                if(prodList.length > 0){
                    item = prodList[0];
                    c = shop.getProdByName(item);
                    isAGo = true;
                }
            }   

            if(c != null){
                if(c.getColumnIndex("_id") >= 0){
                    Product product = new Product();

                    product.setId(c.getInt(c.getColumnIndex("_id")));
                    product.setBarcode(c.getString(c.getColumnIndex("barcode")));
                    product.setName(c.getString(c.getColumnIndex("name")));
                    product.setDesc(c.getString(c.getColumnIndex("desc")));
                    product.setPrice(c.getFloat(c.getColumnIndex("price")));
                    product.setCat(c.getInt(c.getColumnIndex("catid")));

                    cart.AddProduct(product);
                }else{
                    alertbox("Not Found", "Product not found");
                }
            }else{
                alertbox("Error", "Unable to retrieve product.");
            }

            textView.setText("");
            ((ProductAdapter) inventoryList.getAdapter()).notifyDataSetChanged();
        }
    });

    textView.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {

                prodList = ProductDatabase.helper.fetchItemsByName(s.toString());
                if(prodList != null){
                    Autoadapter.clear();
                    for (int i = 0; i < prodList.length; i++)
                    {
                        Log.v("Items", "Item: " + prodList[i]);
                        Autoadapter.add(prodList[i]);
                    }
                }else{
                    Autoadapter.clear();
                }
        }
    });