SQLite数据库崩溃我的应用程序

时间:2016-04-13 09:03:46

标签: android sqlite android-sqlite

在关注youtube教程之后,我编写了一些应该创建数据库和表的代码。

当我加载应用程序并单击Magic Words按钮时,应用程序崩溃并关闭 数据库也没有创建,但我认为这是因为应用程序崩溃了。

任何想法为什么会崩溃?

魔术词

public class Magic_Words extends AppCompatActivity {
    MyDBHelper myDb;
    EditText edittxt_Ring, edittxt_Locate, edittxt_Lock, edittxt_Wipe;
    Button btn_Save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magic__words);
        //myDb = new DataBaseHelper(this);
        myDb = new MyDBHelper(this);

        edittxt_Ring = (EditText) findViewById(R.id.txt_Ring);
        edittxt_Locate = (EditText) findViewById(R.id.txt_Locate);
        edittxt_Lock = (EditText) findViewById(R.id.txt_Lock);
        edittxt_Wipe = (EditText) findViewById(R.id.txt_Wipe);
        Add_Data();
    }

    public void Add_Data() {
        btn_Save.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isInserted = myDb.insertMagicWords(edittxt_Ring.getText().toString());
                        if (isInserted == true)
                            Toast.makeText(Magic_Words.this, "Magic Words Saved", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(Magic_Words.this, "Error Saveng Your Magic Words.", Toast.LENGTH_LONG).show();

                    }
                }

        );
    }
}

BDHelper

//public class MyDBHandler extends SQLiteOpenHelper {

    public class MyDBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "TextIt.db";
    public static final String TABLE_NAME = "MagicWords";
    public static final String ColumnID = "ID";
    public static final String ColumnCommand = "Word";


    public MyDBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (" + ColumnCommand + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                ColumnCommand + "TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertMagicWords(String txt_Ring) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(ColumnCommand, txt_Ring);
        long result = db.insert(TABLE_NAME,null, contentValues);
        if (result == -1)
            return false;
        else
            return true;

    }
}

堆栈跟踪(我认为)

04-13 08:56:58.967 17258-17258/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.kieran.textit, PID: 17258
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kieran.textit/com.kieran.textit.Magic_Words}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                       at com.kieran.textit.Magic_Words.Add_Data(Magic_Words.java:37)
                                                       at com.kieran.textit.Magic_Words.onCreate(Magic_Words.java:33)
                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:148) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

2 个答案:

答案 0 :(得分:2)

这是因为你的按钮变空了。

@Entity @Table(name = "...") public class User extends DbObject { }

上添加此内容
onCreate

答案 1 :(得分:0)

您的创建表查询不正确。您应该在列名称和数据类型之间放置空格,还要记住您在查询中使用的大括号

创建查询的语法

Create table TABLE_NAME (ID Integer Primary Key Autoincrement, COLUMN_1 Text, COLUMN_2 Text)

根据你的情况

<强>替换

db.execSQL("create table " + TABLE_NAME + " (" + ColumnCommand + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," +
                ColumnCommand + "TEXT)");

。通过

db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + ColumnCommand + " TEXT)");

此外,您应该检查所有正确初始化的小部件,就像上面的问题一样,您缺少btnSave初始化(根据您的logcat导致nullPointerException),所以添加它

btnSave = (Button) findViewById(R.id.btnSave);

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

你的logcat说明了一切