Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在多个活动中使用sqlcipher?_Android_Sqlcipher - Fatal编程技术网

Android 如何在多个活动中使用sqlcipher?

Android 如何在多个活动中使用sqlcipher?,android,sqlcipher,Android,Sqlcipher,我第一次使用SQLCipher开发了一个Android应用程序。 我使用Android Studio,创建了数据库,但当我尝试从另一个活动打开SQLCipher时,“getinstance”一词变为红色 您能告诉我如何使用sqlCipher并更正我的代码吗 MainActivity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我第一次使用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);
}
}
    @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);*/
        }
});
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.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);
}
}
    @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);
谢谢你的帮助

db = new DBHelper.getInstance(this);
这不是有效的Java语法。它也不同于您在
main活动中使用的:

db = DBHelper.getInstance(this);
因此,删除
new
关键字

这不是有效的Java语法。它也不同于您在
main活动中使用的:

db = DBHelper.getInstance(this);
所以,删除
new
关键字。

哦,是的,这是一个很大的错误;)。。。非常感谢您的帮助CommonsWareOh,是的,这是一个很大的错误;)。。。非常感谢您对Commonware的帮助