Java 将数据插入SQLite时应用程序崩溃

Java 将数据插入SQLite时应用程序崩溃,java,android,sqlite,Java,Android,Sqlite,我是android新手,请帮我找出代码中的错误 import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class Db_Helper extends SQLiteOp

我是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);


            }
        });

    }




}
IngredinOperations.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);


            }
        });




    }



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


            }
        });




    }



}
RecipeOperations.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);


            }
        });




    }



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


            }
        });




    }



}
当我试图按下按钮时,应用程序崩溃 请告诉我这个错误的可能解决方案 我完全是安卓领域的新手
在advace中感谢您尽管没有logcat(所以我不确定),但我想这是因为在您的
SQLController中插入数据
您的
数据库
为空,因为您没有调用
SQLController\open
。这可能是错误吗?

您没有初始化任何变量。因此它会抛出
NullPointerEXception

在下面的行中,您未初始化
db\u helper

db=db_helper.getWritableDatabase();
这里您没有初始化
sql\u controlr

sql_controlr.insert_Rec_Data(Valid_r_name, Valid_r_ingredients, Valid_r_prepare, Valid_r_servings);

logcat中的崩溃日志是什么?'db=db\u helper.getWritableDatabase()'与SQLController的作用相同。open()否??否,因为保存在
SQLController
中的数据库实例尚未初始化。您需要调用
SQLController.open()
而不是
db=db\u helper.getWritableDatabase()
谢谢您的帮助我如何初始化它我对Android完全陌生,使用了新的操作符,比如Db_Helper Db_Helper=new Db_Helper();用onCreate()方法初始化。很抱歉,我的网络出现了一些问题。我仍然有同样的问题。非常感谢您,先生。。。它只是起作用了。。它的d问题与初始化现在还有一个问题,我可以使用这个新创建的数据库到一些其他项目??怎么做dat???