after bootup,crashing

北战南征 提交于 2019-12-19 11:32:52

问题


My application is using a bootup service.In the service i have to get values from a database,but the app is crashing as it tries getting the values...

The logcat error is listed below...........

01-03 22:19:46.097: E/AndroidRuntime(240): FATAL EXCEPTION: main 01-03 22:19:46.097: E/AndroidRuntime(240): java.lang.RuntimeException: Unable to create service com.android.antitheft.MyService: java.lang.IllegalStateException: get field slot from row 0 col -1 failed 01-03 22:19:46.097: E/AndroidRuntime(240): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2969) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.app.ActivityThread.access$3300(ActivityThread.java:125) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.os.Handler.dispatchMessage(Handler.java:99) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.os.Looper.loop(Looper.java:123) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.app.ActivityThread.main(ActivityThread.java:4627) 01-03 22:19:46.097: E/AndroidRuntime(240): at java.lang.reflect.Method.invokeNative(Native Method) 01-03 22:19:46.097: E/AndroidRuntime(240): at java.lang.reflect.Method.invoke(Method.java:521) 01-03 22:19:46.097: E/AndroidRuntime(240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-03 22:19:46.097: E/AndroidRuntime(240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 01-03 22:19:46.097: E/AndroidRuntime(240): at dalvik.system.NativeStart.main(Native Method) 01-03 22:19:46.097: E/AndroidRuntime(240): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed 01-03 22:19:46.097: E/AndroidRuntime(240): at android.database.CursorWindow.getString_native(Native Method) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.database.CursorWindow.getString(CursorWindow.java:329) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 01-03 22:19:46.097: E/AndroidRuntime(240): at com.android.antitheft.MyService.onCreate(MyService.java:69) 01-03 22:19:46.097: E/AndroidRuntime(240): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959) 01-03 22:19:46.097: E/AndroidRuntime(240): ... 10 more

Thanx in advance..............

my service.java //file

package com.android.antitheft;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

SQLiteDatabase myDB;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void onCreate() {
    super.onCreate();
    System.out.println("@@@@@@@@@@@OnCreate1111");
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();


      myDB = MyService.this.openOrCreateDatabase("antitheft", MODE_PRIVATE, null);
        Cursor cursor1 = myDB.query("SimSerial", null, null, null, null, null, null);
     cursor1.moveToLast();
        //cursor1.moveToFirst();
        String ss1 = cursor1.getString(cursor1.getColumnIndex("simno"));
        Toast.makeText(this, "FROM DATABASE: SIM SERIAL "+ss1, Toast.LENGTH_LONG).show();
        cursor1.close();
        myDB.close();



//          cursor2.moveToNext();
//          String num2 = cursor2.getString(cursor2.getColumnIndex("secure"));
//          
//          cursor2.moveToNext();
//          String num3 = cursor2.getString(cursor2.getColumnIndex("secure"));  

        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String ss=tm.getSimSerialNumber();

        Log.v("SERVICE", ss);

        Toast.makeText(this, "Service started : SIM SERIAL "+ss, Toast.LENGTH_LONG).show();

        Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");          



        if(ss.equals(ss1))
        {
            SmsManager sms = SmsManager.getDefault();

//              sms.sendTextMessage("5556", null, "message", null, null);

            Toast.makeText(this, "INSIDE iffffffffffff ", Toast.LENGTH_LONG).show();
             myDB = MyService.this.openOrCreateDatabase("antitheft", MODE_PRIVATE, null);
            Cursor cursor2 = myDB.query("SimSerial", null, null, null, null, null, null);

            cursor2.moveToFirst();
            while (cursor2.isAfterLast() == false) {
                String num1 = cursor2.getString(cursor2.getColumnIndex("secure"));
                sms.sendTextMessage(num1, null, "message", null, null);
                cursor2.moveToNext();
            }
            cursor2.close();

            myDB.close();

//              sms.sendTextMessage(num1, null, "message", null, null);  
//              sms.sendTextMessage(num2, null, "message", null, null);
//              sms.sendTextMessage(num3, null, "message", null, null);
            }

//          else
//              
//          {
//              SmsManager sms = SmsManager.getDefault();
//              sms.sendTextMessage("5556", null, "message", null, null);
//              
//              Toast.makeText(this, "INSIDE elseeeeeeeeeeeeee", Toast.LENGTH_LONG).show();
//          }


}

@Override
public void onDestroy() {
    super.onDestroy();

    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

}

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {



        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

}


回答1:


When you Create Cursor from the Database you have to first check for both (cursor != null && cursor.moveToFirst()). In your case if there is no any data available then it will create a problem.

so before you use Cursor add the condition

if(c != null && c.moveToFirst()) {
//write your code here
} 

java.lang.IllegalStateException: get field slot from row 0 col -1 failed

Normally this error you will get if you are trying to read a column value from the cursor but that column is not exists in the cursor.




回答2:


From the error it seems that you are fetching some content from the Database in the Cursor. But fetching the content from the Cursor you need to point the Cursor to the first row that your cursor contains. So you need to apply cursor.moveToFirst(); to get it working, if this is the case. Else you should post the full source or the code where you feel its crashing.




回答3:


The error says that you are trying to receive the string from the column with index -1 which is WRONG. Columns are enumerated starting from 0. As google's doc says about Cursor.getColumnIndex(String name), it returns -1 if the column with "name" name doesn't exist. Check the line 69 of your MyService.java file:

String num1 = cursor2.getString(cursor2.getColumnIndex("secure"));

I think there is no "secure" column in your database. Verify your database.




回答4:


I found the answer for this myself...

the error,rather say;my mistake in coding was that i was supposed to use moveToNext() instead of isAfterLast()




回答5:


Where is the database located? And are you sure you have your permissions set? Also, could you post the code to your MyService Class?



来源:https://stackoverflow.com/questions/8722853/after-bootup-crashing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!