2016-09-17 35 views
-1

我试图做一些数据库操作时广播(CONNECTIVITY_CHANGE)发生时,我的应用程序运行时其工作正常时,但应用程序被杀害后,给出了异常:Access数据库提供了NPE

09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err: java.lang.NullPointerException 
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at com.example.deepak.broadcastrecieverswithdatabase.Operations.doInBackground(Operations.java:43) 
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at com.example.deepak.broadcastrecieverswithdatabase.Operations.doInBackground(Operations.java:14) 
09-17 12:10:01.175 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:288) 
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
09-17 12:10:01.185 31502-31515/com.example.deepak.broadcastrecieverswithdatabase W/System.err:  at java.lang.Thread.run(Thread.java:841) 

我最近开始开发android应用程序,任何人都可以帮我解决这个问题吗?还有任何优化我的代码的提示将不胜感激。 我的代码是这样的:

package com.example.deepak.broadcastrecieverswithdatabase; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.os.AsyncTask; 

import java.util.ArrayList; 
import java.util.List; 


public class Operations extends AsyncTask<String, String, List<Integer>> { 
DBHelper dbHelper; 
SQLiteDatabase db; 
Cursor cursor; 
List<Integer> counterList; 
public static final String DB_NAME = "db_name.db"; 
public static final int VERSION = 1; 
public static final String TABLE = "counters"; 
public static final String COUNTER1 = "counter1"; 
public static final String COUNTER2 = "counter2"; 
String opType = ""; 
Context context = null; 
String operation; 
int counter1, counter2; 
ContentValues contentValues; 

public Operations(MainActivity activity, String operation, String opType, int counter1, int counter2){ 
    if(activity!=null) 
     this.context = activity.getApplicationContext(); 
    this.operation = operation; 
    this.counter1 = counter1; 
    this.counter2 = counter2; 
    this.opType = opType; 
} 

@Override 
protected List<Integer> doInBackground(String... strings) { 
    try { 
     dbHelper = new DBHelper(); 
     db = dbHelper.getWritableDatabase(); 

     switch (operation) { 
      case "insert": 
       contentValues = new ContentValues(); 
       contentValues.put(COUNTER1, counter1); 
       contentValues.put(COUNTER2, counter2); 

       db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_IGNORE); 
       break; 

      case "update": 
       counterList = new ArrayList<>(); 
       Cursor cursor1 = getCursor(); 
       try { 
        cursor.moveToFirst(); 
        while (!cursor.isAfterLast()) { 
         counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter1")))); 
         counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter2")))); 
         cursor.moveToNext(); 
        } 
       } finally { 
        cursor.close(); 
       } 
       counter1 = counterList.get(0); 
       counter2 = counterList.get(1); 
       if(opType.equals("alwaysOn")) 
       { 
        counter1++; 
       } 
       else 
       { 
        counter2++; 
       } 
       if (counter1 > 0) { 
        contentValues = new ContentValues(); 
        contentValues.put(COUNTER1, counter1); 
        contentValues.put(COUNTER2, counter2); 
        db.update(TABLE, contentValues, COUNTER1 + "=" + (counter1 - 1), null); 
       } 
       List<Integer> counters = new ArrayList<>(); 
       counters.add(counter1); 
       counters.add(counter2); 
       return counters; 

      case "get": 
       counterList = new ArrayList<>(); 
       Cursor cursor = getCursor(); 
       try { 
        cursor.moveToFirst(); 
        while (!cursor.isAfterLast()) { 
         counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter1")))); 
         counterList.add(Integer.parseInt(cursor.getString(cursor.getColumnIndex("counter2")))); 
         cursor.moveToNext(); 
        } 
        return counterList; 
       } finally { 
        cursor.close(); 
       } 
     } 
    }catch (Exception npe) 
    {npe.printStackTrace();} finally { 

     dbHelper.close(); 
    } 
    return null; 
} 

public Cursor getCursor(){ 
    cursor = db.query(TABLE, null, null, null, null, null, null); 
    return cursor; 
} 

@Override 
protected void onPostExecute(List<Integer> counterList) { 
    super.onPostExecute(counterList); 
    if(counterList!=null && context!=null) { 
     MainActivity.alwaysOnValue.setText(""+counterList.get(0)); 
     MainActivity.onDemandValue.setText(""+counterList.get(1)); 
    } 
} 

class DBHelper extends SQLiteOpenHelper { 

    public DBHelper() { 
     super(context, DB_NAME, null, VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
     String sql = String.format("create table %s"+"(%s int primary key, %s int)", TABLE, COUNTER1,COUNTER2); 
     sqLiteDatabase.execSQL(sql); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { 
     sqLiteDatabase.execSQL("drop table if exists "+ TABLE); 
     onCreate(sqLiteDatabase); 
    } 
} 
} 
+0

没有帮助, –

+0

因为你仍然不知道如何管理的NPE。了解NPE是什么以及为什么会发生。然后你会看到光。 –

+1

我试图寻找这个,经过一段时间的搜索,我发现NPE的原因(早先我发送活动,但是当应用程序被杀死时,不会有任何我没有想到的活动,但广播接收器的受限上下文将在那里,所以现在我发送,这解决了我的问题),现在我纠正它,谢谢,但对于学习者(如我),你应该是一个更详细一点,谢谢反正家伙。 –

回答

0

把所有的工作,在服务和包装箱数据库实例的服务

+0

谢谢,我会尝试。 –