每当我运行代码应用程序在实例化助手类后崩溃。我是新手,无法弄清楚错误。
我的合同类
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);
}
}
答案 0 :(得分:0)
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
onCreate(db);
}
这是一个递归函数!!
请勿打电话给创建内部onCreate