我正在尝试获取调用我的接收器的活动的上下文。我想在我的测试用例类中使用上下文。我写了这样的测试用例。
public class TestDataupload extends AndroidTestCase {
public static DataUpload dat;
public static SyncUpload syn;
protected void setUp() throws Exception {
try {
super.setUp();
syn = new SyncUpload();
dat = new DataUpload(syn.returncon());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testupload() throws Exception {
long lst = dat.insert("Down", "com.android", "2012-02-18 00:00:00",
"0000-00-00 00:00:00", "0");
Assert.assertEquals(2, lst);
}
我的接收器课程在这里
public class SyncUpload extends Activity {
DataUpload du;
List<String> records;
InputStream is;
ArrayList<NameValuePair> nameValuePairs;
String result;
public static final String TAG = "test";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (du == null)
du = new DataUpload(getApplicationContext());
insertAppList();
getAppList();
}
private void insertAppList() {
if (du == null)
du = new DataUpload(this);
List<PackageInfo> packs = this.getPackageManager()
.getInstalledPackages(0);
String[] appName = new String[packs.size()];
String[] packName = new String[packs.size()];
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
appName[i] = p.applicationInfo.loadLabel(this.getPackageManager())
.toString();
packName[i] = p.packageName;
du.insertApp(appName, packName);
}
private void getAppList() {
if (du == null)
du = new DataUpload(this);
if(records == null)
records= du.select();
if(records.size() > 0){
//for (String record : records)
// Log.d(TAG, "RECORDS" + record);
Log.d(TAG, "RECORDS" + records.size());
}
@Override
public void finish() {
super.finish();
du.close();
}
public static Context CTX;
public void getctx(Context context){
CTX = context;
}
public Context returncontext(){
return CTX;
}}
SyncUpload是我的活动类,我在其中创建了returncontext()方法,该方法返回活动的上下文。但我得到一个空指针异常.error log是
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase
(SQLiteOpenHelper.java:118)
at com.android.sync.DataUpload.<init>(DataUpload.java:45)
at com.android.sync.test.TestActivity.setUp(TestActivity.java:27)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
我应该如何摆脱这个错误。有没有其他技术可以解决这个问题。提前致谢。
公共类DataUpload {
public static final String TAG = "test";
private static final String DATABASE_NAME = "deviceintelligenceapplist.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "app_list";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private OpenHelper openHelper;
private static final String INSERT = "insert into "
+ TABLE_NAME
+ "(app_name, package_name, added_date_time, removed_date_time, flag)"
+ "values (?,?,?,?,?)";
private String recordList;
public DataUpload(Context context) {
Log.d(TAG, "INSIDE APPLISTDATA CONSTRUCTOR");
this.context = context;
openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
Log.d(TAG, "END OF APPLISTDATA CONSTRUCTOR");
}
public long insert(String app_name, String package_name,
String added_date_time, String removed_date_time, String flag) {
Log.d(TAG, "START OF INSERT FUNCTION IN DATAUPLOAD");
this.insertStmt.bindString(1, app_name);
this.insertStmt.bindString(2, package_name);
this.insertStmt.bindString(3, added_date_time);
this.insertStmt.bindString(4, removed_date_time);
this.insertStmt.bindString(5, flag);
Log.d(TAG, "END OF INSERT FUNCTION IN DATAUPLOAD");
long value = this.insertStmt.executeInsert();
Log.d(TAG, "Database total inserts:---" + value);
return value;
}
public long insertApp(String[] app_name, String[] package_name) {
Log.d(TAG, "START OF INSERT FUNCTION IN DATAUPLOAD");
long value = 0;
String flag = "0";
String remove_date_time = "0000-00-00 00:00:00.000";
SimpleDateFormat sdfDateTime = new SimpleDateFormat(
"yy-MM-dd HH:mm:ss", Locale.US);
String added_date_time = sdfDateTime.format(System.currentTimeMillis());
for (int i = 0; i < package_name.length; i++) {
this.insertStmt.bindString(1, app_name[i]);
this.insertStmt.bindString(2, package_name[i]);
this.insertStmt.bindString(3, added_date_time);
this.insertStmt.bindString(4, remove_date_time);
this.insertStmt.bindString(5, flag);
value = this.insertStmt.executeInsert();
Log.d(TAG, "insertapp: " + value);
}
Log.d(TAG, "END OF INSERT FUNCTION IN DATAUPLOAD");
return value;
}
public void deleteAll() {
Log.d(TAG, "START OF DELETEALL FUNCTION IN DATAUPLOAD");
db.execSQL("DELETE FROM " + TABLE_NAME
+ " WHERE removed_date != 0000-00-00 00:00:00.000");
Log.d(TAG, "END OF DELETEALL FUNCTION IN APPLISTDATA");
}
public List<String> selectAll() {
Log.d(TAG, "START OF SELECTALL FUNCTION IN DATAUPLOAD");
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, null, "flag=?",
new String[] { "0" }, null, null, null, null);
if (cursor.moveToFirst()) {
do {
recordList = cursor.getString(0) + " /; " + cursor.getString(1)
+ " /; " + cursor.getString(2) + " /; "
+ cursor.getString(3) + " /; " + cursor.getString(4)
+ "\n";
list.add(recordList);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
Log.d(TAG, "APPLISTDATA: " + list.toString());
Log.d(TAG, "END OF SELECTALL FUNCTION IN DATAUPLOAD");
return list;
}
public List<String> select() {
Log.d(TAG, "START OF SELECTALL FUNCTION IN DATAUPLOAD");
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null,
null);
if (cursor.moveToFirst()) {
do {
recordList = cursor.getString(0) + " /; " + cursor.getString(1)
+ " /; " + cursor.getString(2) + " /; "
+ cursor.getString(3) + " /; " + cursor.getString(4)
+ "\n";
list.add(recordList);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
Log.d(TAG, "END OF SELECTALL FUNCTION IN DATAUPLOAD");
return list;
}
public void close() {
Log.d(TAG, "START OF CLOSE FUNCTION IN DATAUPLOAD");
// OpenHelper openHelper = new OpenHelper(this.context);
openHelper.close();
Log.d(TAG, "END OF CLOSE FUNCTION IN DATAUPLOAD");
}
public void update() {
Log.d(TAG, "START OF LAST RECORD UPDATE IN DATAUPLOAD");
String selectflag = "0";
long count = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
if (count > 0) {
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "flag" },
"flag= ?", new String[] { selectflag }, null, null, null,
null);
while (cursor.moveToNext()) {
Log.d(TAG, "flag: while " + cursor.getString(0).trim());
if (cursor.getString(0).trim().equals("0")) {
Log.d(TAG, "flag: if ");
ContentValues args = new ContentValues();
args.put("flag", "1");
db.update(TABLE_NAME, args, "flag = 0", null);
}
}
Log.d(TAG, "SELECTIN UPDATE DB: " + select().toString());
Log.d(TAG, "END OF LAST RECORD UPDATE IN DATAUPLOAD");
}
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "START OF OPENHELPER ONCREATE IN DATAUPLOAD");
db.execSQL("CREATE TABLE "
+ TABLE_NAME
+ "(app_name VARCHAR(40),"
+ "package_name VARCHAR(40), added_date_time VARCHAR(20), removed_date_time VARCHAR(20), "
+ " flag VARCHAR(1))");
Log.d(TAG, "START OF OPENHELPER ONCREATE IN DATAUPLOAD");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "INSIDE UPGRADE IN OPENHELPER");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
@Override
public synchronized void close() {
Log.d(TAG, "INSIDE CLOSE IN OPENHELPER IN DATAUPLOAD");
super.close();
}
}
}
这是数据上传.java