Android:索引1处的绑定值为空错误

Posted

技术标签:

【中文标题】Android:索引1处的绑定值为空错误【英文标题】:Android: the bind value at index 1 is null error 【发布时间】:2015-06-19 14:42:50 【问题描述】:

我正在开发一个应用程序,我在其中运行后台服务:

startService(new Intent(getApplicationContext(), ContactService.class));

在清单中:

<service android:name=".ContactService" ></service>

而服务代码如下:

public class ContactService extends Service 
    private int mContactCount;
    @Override
    public IBinder onBind(Intent arg0) 
        return null;
    

    @Override
    public void onCreate() 
        super.onCreate();
        // Get contact count at start of service
        mContactCount = getContactCount();
        this.getContentResolver().registerContentObserver(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, mObserver);
        Cursor curval = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        if (curval != null && curval.getCount() > 0) 
            curval.getCount();
        
        curval.close();
    

    private int getContactCount() 
        try 
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) 
                return cursor.getCount();
             else 
                cursor.close();
                return 0;
            
         catch (Exception ignore) 
         finally 
            cursor.close();
        
        return 0;
    

    private ContentObserver mObserver = new ContentObserver(new Handler()) 
        @Override
        public void onChange(boolean selfChange) 
            new changeInContact().execute();
        
    ;

    public class changeInContact extends AsyncTask<String, Void, String> 
        @Override
        protected void onPreExecute() 
            super.onPreExecute();
        

        @Override
        protected String doInBackground(String... arg0) 
            ArrayList<Integer> arrayListContactID = new ArrayList<Integer>();
            int currentCount = getContactCount();
            // Add contact
            if (currentCount > mContactCount) 
                // Adding the contact if max contact id from device to sqlite database
             else if (currentCount < mContactCount)   
                // Delete contact from sqlite database which is not available in device
             else if (currentCount == mContactCount) 
                // Check version of contact. If updated change info from device to sqlite
            

            mContactCount = currentCount;
            return "";
        

        @Override
        protected void onPostExecute(String result) 
        
    

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
        return Service.START_STICKY;
    

当我的服务在后台运行时,我收到错误并且我的应用程序崩溃。错误如下:

java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
at android.content.ContentResolver.query(ContentResolver.java:372)
at android.content.ContentResolver.query(ContentResolver.java:315)
at com.demo.example.ContactService$changeInContact.doInBackground(ContactService.java:577)
at com.demo.example.ContactService$changeInContact.doInBackground(ContactService.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
java.lang.IllegalArgumentException: the bind value at index 1 is null at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
at android.content.ContentResolver.query(ContentResolver.java:372)
at android.content.ContentResolver.query(ContentResolver.java:315)
at com.demo.example.ContactService$changeInContact.doInBackground(ContactService.java:577)
at com.demo.example.ContactService$changeInContact.doInBackground(ContactService.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)

我不明白出了什么问题。请向我提出您的宝贵建议。由于这个原因,我在我的应用程序中非常糟糕。请给我你的意见。

【问题讨论】:

ContactService.java:577 上有什么内容? 为什么onStartCommand在AsyncTask里面? @iago:那是拼写错误。如果异步任务,onStart 就退出了。立即查看更新。 @RohanKandwal ContactService 是我的后台服务。 选择争论为null时发生此错误,您是否可以确保每次执行该特定查询时null not null? span> 【参考方案1】:

the bind value 只不过是queryselectionArgs。如果selectionArgs 在任何时候为空,您将在触发query 时收到此错误。

【讨论】:

以上是关于Android:索引1处的绑定值为空错误的主要内容,如果未能解决你的问题,请参考以下文章

数据绑定值为空时如何在 DataGridViewComboBoxColumn 中显示文本

选择排序

五分钟理解选择排序算法

C语言 — 选择排序法

数组相关问题的时间复杂度

排序--02---选择排序