2016-10-30 1 views
0

Ich entwickle zum ersten Mal eine Android-App mit SQLCipher. Ich benutze Android Studio, die Datenbank wird erstellt, aber wenn ich versuche, SQLCipher von einer anderen Aktivität zu öffnen, wird das Wort "getinstance" rot.Wie verwenden Sie sqlcipher mit mehreren Aktivitäten?

Können Sie mir sagen, wie sqlCipher verwenden und bitte meinen Code korrigieren?

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);*/ 
     } 
}); 

Der Fehler auf der Strecke passiert ist:

db = new DBHelper.getInstance(this); 

Vielen Dank für Ihre Hilfe.

Antwort

0
db = new DBHelper.getInstance(this); 

Dies ist keine gültige Java-Syntax. Es ist auch anders als das, was Sie in MainActivity verwendet:

db = DBHelper.getInstance(this); 

Also, das new Schlüsselwort löschen.

+0

Oh, ja es war ein großer Fehler;) ... Vielen Dank für Ihre Hilfe CommonsWare –

Verwandte Themen