2016-10-30 10 views
0

私は初めてSQLCipherを使用してAndroidアプリを開発しています。 Android Studioを使用するとデータベースが作成されますが、別のアクティビティからSQLCipherを開くと、「getinstance」という単語が赤くなります。複数のアクティビティでsqlcipherを使用するにはどうすればよいですか?

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
db = new DBHelper.getInstance(this); 

これは有効なJava構文ではありません。また、あなたがMainActivityで使用されるものとは異なります。

ので
db = DBHelper.getInstance(this); 

newキーワードを削除します。

+0

ああ、はい、大きなエラーでした;)...ありがとうございましたCommonsWare –

関連する問題