Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带SimpleCorsOrAdapter的AutoCompleteTextView在筛选时返回NullPointerException_Android_Android Sqlite_Simplecursoradapter_Autocompletetextview - Fatal编程技术网

Android 带SimpleCorsOrAdapter的AutoCompleteTextView在筛选时返回NullPointerException

Android 带SimpleCorsOrAdapter的AutoCompleteTextView在筛选时返回NullPointerException,android,android-sqlite,simplecursoradapter,autocompletetextview,Android,Android Sqlite,Simplecursoradapter,Autocompletetextview,我有一个AutoCompleteTextView,我用SimpleCorsOrAdapter链接了它。一切正常,但当用户输入数据库中不存在的字符时,筛选方法返回空光标。这将导致非致命的NullPointerException。我的代码是 mAdapter.setFilterQueryProvider(new FilterQueryProvider() { @Override public Cursor runQuery(CharSequence constrain

我有一个AutoCompleteTextView,我用SimpleCorsOrAdapter链接了它。一切正常,但当用户输入数据库中不存在的字符时,筛选方法返回空光标。这将导致非致命的NullPointerException。我的代码是

mAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence constraint) {
            String partial = null;
            if(constraint!=null)
               partial = constraint.toString();
            Cursor result= dbHelper.readProductCategory(partial);
            return result;
        }
    });
logcat中的警告

 W/Filter﹕ An exception occured during performFiltering()!
java.lang.NullPointerException
        at com.orderoapp.ordero.data.ProductDatabaseHelper.readProductCategory(ProductDatabaseHelper.java:70)
        at com.orderoapp.ordero.OrdersFragment$1.runQuery(OrdersFragment.java:87)
        at android.support.v4.widget.CursorAdapter.runQueryOnBackgroundThread(CursorAdapter.java:397)
        at android.support.v4.widget.CursorFilter.performFiltering(CursorFilter.java:50)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)
我不知道如果光标为空,该怎么办。我应该返回null吗?处理这个问题的最好方法是什么。 另外,当用户删除AutoCompleteTextview中的文本时,我得到了这个错误

E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

有什么想法吗?我非常感谢你的帮助。谢谢。

错误是由于

  ProductDatabaseHelper.readProductCategory 
方法。如果条件是

if ( con.length() > 0 ) {

        result = getReadableDatabase().query(ProductEntry.TABLE_NAME, projection,
               ....
    }
因此,当con为null时,它会给出NullPointerException。解决方案非常简单。在检查con.length()是否大于0之前,只需检查con是否不为null

if ( con!=null && con.length() > 0 ) {
...
    }