我是android的新手,请帮我解决我的代码中的错误
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Db_Helper extends SQLiteOpenHelper{
public static final boolean DEBUG = true;
//Logcat TAG
public static final String LOG_TAG = "DBAdapter";
// DATABASE
static final String DATABASE_NAME = "MYKITCHENBOY";
static final int DATABASE_VERSION = 1;
// TABLE INFORMATTION
public static final String TABLE_INGREDIENTS = "ingredient_table";
public static final String TABLE_RECIPE = "recipe_table";
public static final String KEY_INGREDIENT_ID = "i_id";
public static final String KEY_INGREDIENT_NAME = "i_name";
public static final String KEY_INGREDIENT_IMAGE_NAME="i_image_name";
public static final String KEY_INGREDIENT_DESCRIPTION= "i_description";
public static final String KEY_RECIPE_ID = "r_id";
public static final String KEY_RECIPE_NAME = "r_name";
public static final String KEY_RECIPE_INGREDIENTS="r_ingredients";
public static final String KEY_RECIPE_HOWTO ="r_howto";
public static final String KEY_RECIPE_SERVE_NUM = "r_serv_num";
// INGREDIENT TABLE CREATION STATEMENT
private static final String CREATE_INGREDIENT_TABLE = "create table "
+ TABLE_INGREDIENTS + "(" + KEY_INGREDIENT_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENT_NAME + " TEXT NOT NULL, "+KEY_INGREDIENT_IMAGE_NAME+ " TEXT NOT NULL, " +KEY_INGREDIENT_DESCRIPTION+" TEXT NOT NULL);";
// INGREDIENT TABLE CREATION STATEMENT
private static final String CREATE_RECIPET_TABLE = "create table "
+ TABLE_RECIPE + "(" + KEY_RECIPE_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_RECIPE_NAME + " TEXT NOT NULL, "
+KEY_RECIPE_INGREDIENTS+ " TEXT NOT NULL, "
+KEY_RECIPE_HOWTO+" TEXT NOT NULL,"+
KEY_RECIPE_SERVE_NUM+" TEXT NOT NULL):" ;
public Db_Helper(Context context) {
super(context, DATABASE_NAME, null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
if (DEBUG)
Log.i(LOG_TAG, "new create");
try {
db.execSQL(CREATE_INGREDIENT_TABLE);
db.execSQL(CREATE_RECIPET_TABLE);
}catch (Exception exception) {
if (DEBUG)
Log.i(LOG_TAG, "Exception onCreate() exception");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists " + TABLE_INGREDIENTS );
db.execSQL("drop table if exists "+TABLE_RECIPE);
onCreate(db);
}
}
SQLController.java
public class SQLController {
private Db_Helper dbhelper;
private Context ourcontext;
private SQLiteDatabase database;
public SQLController(Context c) {
ourcontext = c;
}
public SQLController open() throws SQLException {
dbhelper = new Db_Helper(ourcontext);
database = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public void insert_Ing_Data(String name,String image_name, String description) {
ContentValues cv = new ContentValues();
cv.put(Db_Helper.KEY_INGREDIENT_NAME, name);
cv.put(Db_Helper.KEY_INGREDIENT_IMAGE_NAME, image_name);
cv.put(Db_Helper.KEY_INGREDIENT_DESCRIPTION, description);
database.insert(Db_Helper.TABLE_INGREDIENTS, null, cv);
}
public void insert_Rec_Data(String name,String ingredients, String howto, String servings) {
ContentValues cv = new ContentValues();
cv.put(Db_Helper.KEY_RECIPE_NAME, name);
cv.put(Db_Helper.KEY_RECIPE_INGREDIENTS, ingredients);
cv.put(Db_Helper.KEY_RECIPE_HOWTO, howto);
cv.put(Db_Helper.KEY_RECIPE_SERVE_NUM, servings);
database.insert(Db_Helper.TABLE_INGREDIENTS, null, cv);
}
public Cursor read_Ing_Data() {
String[] allColumns = new String[] { Db_Helper.KEY_INGREDIENT_ID,
Db_Helper.KEY_INGREDIENT_NAME, Db_Helper.KEY_INGREDIENT_DESCRIPTION };
Cursor c = database.query(Db_Helper.TABLE_INGREDIENTS, allColumns, null,
null, null, null, Db_Helper.KEY_INGREDIENT_NAME);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor read_Rec_Data() {
String[] allColumns = new String[] { Db_Helper.KEY_RECIPE_ID,
Db_Helper.KEY_RECIPE_NAME, Db_Helper.KEY_RECIPE_INGREDIENTS,Db_Helper.KEY_RECIPE_HOWTO,Db_Helper.KEY_RECIPE_SERVE_NUM };
Cursor c = database.query(Db_Helper.TABLE_RECIPE, allColumns, null,
null, null, null, Db_Helper.KEY_RECIPE_NAME);
if (c != null) {
c.moveToFirst();
}
return c;
}
}
MainActivity.java
public class MainActivity extends Activity{
Button ingredient,recipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ingredient = (Button) findViewById(R.id.add_ingredients_button);
recipe = (Button) findViewById(R.id.add_recipe_button);
ingredient.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent to_ingredient = new Intent(getApplicationContext(),IngredientOperations.class);
startActivity(to_ingredient);
}
});
recipe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent to_recipe = new Intent(getApplicationContext(), RecipeOperations.class);
startActivity(to_recipe);
}
});
}
}
IngredientOperations.java
public class IngredientOperations extends Activity {
EditText ing_name,ing_image_name,ing_descrptn;
String Valid_name,Valid_image_name, Valid_description;
Db_Helper db_helper;
SQLController sql_controller;
Button save_ing_to_db;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ingredient_layout);
save_ing_to_db = (Button) findViewById(R.id.ing_submit);
ing_name = (EditText)findViewById(R.id.ing_name);
ing_image_name = (EditText) findViewById(R.id.ing_image_name);
ing_descrptn = (EditText) findViewById(R.id.ing_description);
Valid_name = ing_name.getText().toString().trim();
Valid_image_name = ing_image_name.getText().toString().trim();
Valid_description = ing_descrptn.getText().toString().trim();
save_ing_to_db.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db=db_helper.getWritableDatabase();
sql_controller.insert_Ing_Data(Valid_name, Valid_image_name, Valid_description);
db_helper.close();
Intent to_main = new Intent(getApplicationContext(), MainActivity.class);
startActivity(to_main);
}
});
}
}
RecipeOperations.java
public class RecipeOperations extends Activity {
Button save_to_recipe_db;
EditText name,ingredients,prepare,servings;
String Valid_r_name, Valid_r_ingredients, Valid_r_prepare, Valid_r_servings;
SQLController sql_controlr;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe_layout);
name = (EditText) findViewById(R.id.recipe_name);
ingredients = (EditText) findViewById(R.id.recipe_ingredients);
prepare = (EditText) findViewById(R.id.recipe_howto);
servings = (EditText) findViewById(R.id.recipe_serve_num);
Valid_r_name = name.getText().toString().trim();
Valid_r_ingredients = ingredients.getText().toString().trim();
Valid_r_prepare = prepare.getText().toString().trim();
Valid_r_servings = servings.getText().toString().trim();
save_to_recipe_db = (Button) findViewById(R.id.recipe_sumbit);
save_to_recipe_db.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sql_controlr.insert_Rec_Data(Valid_r_name, Valid_r_ingredients, Valid_r_prepare, Valid_r_servings);
sql_controlr.close();
Intent to_main = new Intent(getApplicationContext(),MainActivity.class);
startActivity(to_main);
}
});
}
}
当我尝试按下按钮时,应用程序崩溃了 请告诉我这个错误的可能解决方案 我完全是Android领域的新手 感谢你的推荐
答案 0 :(得分:0)
虽然没有logcat(所以我不确定),我想这是因为在你的SQLController#insert_Ing_Data
中database
为空,因为你没有调用SQLController#open
。这可能是错误吗?
答案 1 :(得分:0)
你没有初始化任何变量。所以它抛出NullPointerEXception
。
在下面一行中,您不会被初始化db_helper
db=db_helper.getWritableDatabase();
此处您尚未初始化sql_controlr
sql_controlr.insert_Rec_Data(Valid_r_name, Valid_r_ingredients, Valid_r_prepare, Valid_r_servings);