我有一个示例应用程序,它创建一个数据库、表并插入一行值。
这是我的MainActivity.java
:
public class MainActivity extends Activity {
protected static final android.content.Context Context = null;
public String ReadingMode="";
@Override
protected void onCreate(Bundle savedInstanceState) {
SQLiteDatabase sampleDB = Context.openOrCreateDatabase("WaterElectricityReading", MODE_WORLD_READABLE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
"SampleTable " +
" (LastName VARCHAR, FirstName VARCHAR," +
" Country VARCHAR, Age INT(3));");
sampleDB.execSQL("INSERT INTO " +
"SampleTable " +
" Values ('Makam','sample','India',25);");
sampleDB.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
当我逐行调试时,在行Null pointer exception
附近提出:
SQLiteDatabase sampleDB = Context.openOrCreateDatabase("WaterElectricityReading", MODE_WORLD_READABLE, null);
我看到那sampleDB
是空的并且Database
没有被创建。
我的代码有什么问题吗?