我制作了一个应用程序,它复制了一个预先制作的数据库,然后允许您以 SQLTest 作为其主要活动来运行 SQLiteDatabase 的各种查询/方法(插入、更新等)。然后我做了一个测试(称为 SQLTestCase,我知道这不合适,但我是测试新手),它有一个测试套件调用 randomTest() 方法,该方法随机选择要执行的测试。奇怪的是,我收到一条错误消息:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.sql2/databases/os.sqlite
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1310)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at com.example.sql2.test.SQLTestCase.randomTest(SQLTestCase.java:236)
at com.example.sql2.test.SQLTestCase.testSuite(SQLTestCase.java:38)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
错误显示在两行代码中:
public void testSuite()
{
int length = 100;
for (int x = 0; x < length; x++)
{
randomTest(); // Here
}
}
public void randomTest()
{
...
String sql = "SELECT COUNT(*) AS NumberOfRecords FROM" + SQLAdapter.TABLE_OS;
Cursor cursor = activity.accessAdapter().accessHelper().accessDatabase().rawQuery(sql, null); //Here
...
我知道问题出在光标的初始化上,因为通过注释掉它,测试运行良好。有谁知道这个问题?我查看了我的代码,希望能找到一个我提前关闭数据库的地方,但我什么也没看到。我会尽力提供你所需要的。让我知道是否需要更多代码来解决此问题。
public class SQLTestCase extends ActivityInstrumentationTestCase2<SQLTest>
{
SQLTest activity;
public SQLTestCase()
{
super(SQLTest.class);
}
protected void setUp() throws Exception
{
super.setUp();
setActivityInitialTouchMode(false);
activity = getActivity();
}
...
public void testSuite()
{
int length = 100;
for (int x = 0; x < length; x++)
{
randomTest();
}
}
...
public void randomTest()
{
String name = "", field = "";
Random rand = new Random();
int randOp = rand.nextInt(5);
int randName, randField, id;
String sql = "SELECT COUNT(*) AS NumberOfRecords FROM" + SQLAdapter.TABLE_OS;
Cursor cursor = activity.accessAdapter().accessHelper().accessDatabase().rawQuery(sql, null);
switch (randOp)
{
case 0:
randName = rand.nextInt(45);
name = getRandomName(randName);
int nullNotNull = rand.nextInt(2);
switch (nullNotNull)
{
case 0:
field = null;
break;
case 1:
randField = rand.nextInt(24);
field = getRandomField(randField);
break;
}
testInsert(name, field);
break;
case 1:
id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
//id = rand.nextInt(12)+1;
testDelete(id);
break;
case 2:
int selection = rand.nextInt(5);
switch (selection)
{
case 0:
randName = rand.nextInt(45);
id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
//id = rand.nextInt(12)+1;
name = getRandomName(randName);
testUpdate(id, name, "");
break;
default:
randName = rand.nextInt(45);
name = getRandomName(randName);
randField = rand.nextInt(24);
field = getRandomField(randField);
id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
//id = rand.nextInt(12)+1;
testUpdate(id, name, field);
break;
}
break;
case 3:
id = rand.nextInt(Integer.parseInt(cursor.getString(0)));
//id = rand.nextInt(12)+1;
testGet(id);
break;
case 4:
testGetAll();
break;
}
}
}
public class SQLTest extends Activity
{
public SQLAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqltest);
adapter = new SQLAdapter(this);
adapter.createDatabase();
open();
}
...
}