2016-09-25 19 views
-1

ヘルパークラスのインスタンスを作成した後、コードアプリケーションを実行するたびにクラッシュします。私はこれで新しく、エラーを理解することはできません。AndroidアプリケーションでgetWritableDatabase()を実行した後でアプリケーションがクラッシュする

私の契約クラス

public final class PetContract { 
    private PetContract(){} 
    public static class PetEntry implements BaseColumns{ 
     public static final String TABLE_NAME = "pets"; 
     public static final String _ID = BaseColumns._ID; 
     public static final String COLUMN_NAME = "name"; 
     public static final String COLUMN_BREED = "breed"; 
     public static final String COLUMN_GENDER = "gender"; 
     public static final String COLUMN_WEIGHT = "weight"; 
     // constants for gender 
     public static final int GENDER_UNKNOWN = 0; 
     public static final int GENDER_MALE = 1; 
     public static final int GENDER_FEMALE = 2; 
    } 
} 

マイSQLiteHelperClass

public class PetDbHelper extends SQLiteOpenHelper { 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "pets.db"; 
    public static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + PetEntry.TABLE_NAME + " (" 
      + PetEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + PetEntry.COLUMN_NAME + " TEXT NOT NULL, " 
      + PetEntry.COLUMN_BREED + " TEXT NOT NULL, " 
      + PetEntry.COLUMN_GENDER + " INTEGER NOT NULL, " 
      + PetEntry.COLUMN_WEIGHT + " FLOAT NOT NULL " 
      + ")"; 
    private static final String SQL_DELETE_ENTRIES = 
      "DROP TABLE IF EXISTS " + PetEntry.TABLE_NAME; 
    public PetDbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
     onCreate(db); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 
} 

主な活動

public class CatalogActivity extends AppCompatActivity { 
    PetDbHelper mDbHelper = new PetDbHelper(this); 
    public void insertDummyData(){ 
     SQLiteDatabase db = mDbHelper.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     String name = "DOG"; 
     String breed = "dkdsbk"; 
     float weight = (float) 55.5; 
     values.put(PetEntry.COLUMN_NAME,name); 
     values.put(PetEntry.COLUMN_BREED, breed); 
     values.put(PetEntry.COLUMN_GENDER,PetEntry.GENDER_MALE); 
     values.put(PetEntry.COLUMN_WEIGHT,weight); 
     long newRowId = db.insert(PetEntry.TABLE_NAME,null,values); 
    } 
    public Cursor displayRowCount(){ 
     SQLiteDatabase db = mDbHelper.getReadableDatabase(); 
     String[] projection = {PetEntry._ID}; 
     String selection = PetEntry.TABLE_NAME; 
     return db.query(PetEntry.TABLE_NAME,projection,null,null,null,null,null); 

    } 


    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_catalog); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent intent = new Intent(CatalogActivity.this,EditorialActivity.class); 
       startActivity(intent); 
      } 
     }); 
     Cursor c = displayRowCount(); 
     TextView view = (TextView) findViewById(R.id.text); 
     view.setText("The row count is" + c.getCount()); 
    } 

    @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_catalog, 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(); 

     //noinspection SimplifiableIfStatement 
     switch (id){ 
      case R.id.delete_all_the_pets: 
       break; 
      case R.id.insert_dummy_data: 
       insertDummyData(); 
       break; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+3

ログの詳細を表示したくありませんか?あなたの質問 –

答えて

0
@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(SQL_CREATE_ENTRIES); 
    onCreate(db); 
} 

これは再帰関数です!

onCreateを呼び出さないでください。作成中

+0

と一緒にログの詳細を追加しないでくださいsuper.onCreate()like this.onCreate() あなたはsuper.onCreate()を呼び出す必要はありません –

関連する問題