无法从警报服务访问sqlite数据库

时间:2015-09-21 04:20:13

标签: android sqlite android-studio

我想从报警服务访问我的sqlite数据库。也想要将新记录添加到数据库。我尝试了很多但是获得了异常。请帮帮我们..你可以找到下面的全部代码

public class MyAlarmService extends Service  {

DatabaseManager database;

    String ename;
    int id;
    DateFormat d;
    RestClient obj;
    private Context context;
    Long timstpAtFive;
    private String posturl = "http://infinitsolutions.in/attendance/api/attendance";

    public MyAlarmService(){}

    public MyAlarmService(Context context, Long tlong ){
        this.context = context;
        this.timstpAtFive=tlong;



    }




    @Override
    public IBinder onBind(Intent arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId)
    {

    super.onStart(intent, startId);
            d = DateFormat.getDateInstance();
            obj = new RestClient((android.app.Activity) context);
            try {

                String stat = "signed in";
                ArrayList<DataRecord>autoList = database.executeQuery("select employee_Id from attendance where date='" + d.format(new Date()) + "' and status = '" + stat + "'");

                int size = autoList.size();
                for (int i = 0; i < size; i++) {

                          id = autoList.get(i).getInt("employee_Id");
        ArrayList<DataRecord> list = database.executeQuery("select status from attendance where employee_Id ='" + id + "'and date='" + d.format(new Date()) + "'");
                        int n = list.size();
                        if (list.get(n - 1).equals("signed in")) {

                        ArrayList<DataRecord> list1 = database.executeQuery("select employee_name from employee where employee_Id ='" + id + "'");
                        ename = list1.get(0).getString("employee_name");
                        methodemployee(id, ename);

                    }
                }
            }catch(NullPointerException e){
                    e.printStackTrace();
            } catch(Exception e){
                e.printStackTrace();
            }
 private void methodemployee(int employee_id,String employee_name)
    {
        DataRecord data= new DataRecord();
        try {
            data.put("employee_name", employee_name);
            data.put("employee_Id",employee_id);
            data.put("date", d.format(new Date()));
            data.put("time",timstpAtFive);
            data.put("status", "signed out");
            data.put("type", "auto");
            database.addRecord("attendance", null, data);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        ArrayList<DataRecord> attendance = database.executeQuery("select * from attendance where employee_Id ='" + employee_id + "'");
        int n=attendance.size();
        //postdata(attendance.get(n-1));

    }




    @Override
    public void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
    // Post data to server and return acknowledgement from server
    private void postdata(DataRecord dataRecord) {
        obj.POST(posturl, getJSON(dataRecord), new RestListenerJson() {
            @Override
            public void onResultJsonFormat(JSONObject jsonobject) {

            }

            @Override
            public void onResultJsonFormat(String jsonobject) {
                String result = jsonobject;

            }
        });
    }
    //Convert the datarecords to JSONString
    private String getJSON(DataRecord dataRecord)

    {
        JSONObject Jobj = new JSONObject();
        try {
            DateFormat d1 = DateFormat.getTimeInstance();
            //long time = d1.parse(dataRecord.getString("time")).getTime() / 1000;


            Jobj.put("date", dataRecord.getString("date"));
            Jobj.put("employee_Id", dataRecord.getString("employee_Id"));
            Jobj.put("time", timstpAtFive);
            Jobj.put("status", dataRecord.getString("status"));
            Jobj.put("employee_name", dataRecord.getString("employee_name"));
            Jobj.put("type", dataRecord.getString("type"));


        } catch (JSONException e) {
            e.printStackTrace();
        } //catch (ParseException e) {
        // e.printStackTrace();
        //}
        catch (NullPointerException e) {
            e.printStackTrace();
        }

        return Jobj.toString();
    }



}

你可以在下面找到logcat警告......没有错误

9-21 11:32:00.877    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ java.lang.NullPointerException
09-21 11:32:00.877    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at com.example.infinitdeveloper.myapplication.MyAlarmService.onStart(MyAlarmService.java:74)
09-21 11:32:00.877    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.app.Service.onStartCommand(Service.java:450)
09-21 11:32:00.877    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2797)
09-21 11:32:00.877    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.app.ActivityThread.access$2100(ActivityThread.java:145)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5214)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:814)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:630)
09-21 11:32:00.887    4778-4778/com.example.infinitdeveloper.myapplication W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您需要从Alarm Manager获取上下文并将其传递给Database Manager类以访问数据库。

修改 您无法在不初始化的情况下执行数据库查询。您需要制作单独的数据库管理器类。在其中编写函数,然后将上下文传递给该函数并从数据库中获取结果。

请注意以下链接:Vogella Android Database。您没有初始化和打开Database intstant。您需要阅读有关Android数据库的一些内容。