我是第一次使用SQLCipher开发Android应用程序。 我使用Android Studio,创建了数据库,但是当我尝试从另一个活动打开SQLCipher时,单词" getinstance"变红了。
你能告诉我如何使用sqlCipher并更正我的代码吗?
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
context = MainActivity.this;
// Load an ad into the AdMob banner view.
/*AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);*/
// Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
/* Enabling sql cipher */
if(DBHelper.enableSQLCypher)
{
SQLiteDatabase.loadLibs(this);
}
db = DBHelper.getInstance(this);
Toast.makeText(context,"database created successfuly",Toast.LENGTH_LONG).show();
ListView ListeCategories = (ListView) findViewById(R.id.list);
TextView CategoriesTitle = (TextView)findViewById(R.id.textViewCategoriesTitle);
CategoriesTitle.setText("THEMES");
registerForContextMenu(ListeCategories);
Cursor categories = db.getCategoriesListByCursor();
CustomCursorAdapter customCursorAdapter = new CustomCursorAdapter(this, categories);
ListeCategories.setAdapter(customCursorAdapter);
ListeCategories.setClickable(true);
db.close();
}
// do not forget to close db instance
@Override
public void onDestroy() {
super.onDestroy();
db.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.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_add) {
Intent intentNewCategorie = new Intent(getApplicationContext(), AddCategorieActivity.class);
intentNewCategorie.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentNewCategorie);
return true;
}
else if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
DBHelper.java
public static boolean enableSQLCypher = true;
/* public DBHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
*/
private DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
DBHelper.context = context;
}
public static synchronized DBHelper getInstance(Context context) {
//public static DBHelper getInstance(Context context) {
if (instance == null) {
instance = new DBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
db = instance.getWritableDatabase(CIPHER_PWD);
}
return instance;
}
/*public class getInstance extends DBHelper {
public getInstance(Context context) {
super();
}
}*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_CATEGORIE + "(" +
CATEGORIE_ID + " INTEGER_PRIMARY_KEY_TYPE," +
CATEGORIE_IMAGE + " BLOB," +
CATEGORIE_NOM + " TEXT," +
CATEGORIE_REMARKS + " TYPE" +
")");
};
AddCategorieActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_categorie);
editCategorieName = (EditText)findViewById(R.id.editTextCategorieName);
editRemarks = (EditText)findViewById(R.id.editTextCategorieRemark);
imgCategorie = (ImageView)findViewById(R.id.imgViewCategorie);
btnAdd = (Button)findViewById(R.id.ButtonAddCategorie);
btnCancel = (Button)findViewById(R.id.ButtonCancelCategorie);
if(DBHelper.enableSQLCypher)
{
SQLiteDatabase.loadLibs(this);
}
db = new DBHelper.getInstance(this);
imgCategorie.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
//Intent intent = new Intent();
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
/*intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);*/
}
});
错误发生在该行:
db = new DBHelper.getInstance(this);
感谢您的帮助。
答案 0 :(得分:0)
db = new DBHelper.getInstance(this);
这不是有效的Java语法。它也与您在MainActivity
中使用的不同:
db = DBHelper.getInstance(this);
因此,请删除new
关键字。