SQLite应用程序在显示数据时崩溃

时间:2017-05-09 13:02:07

标签: java android sqlite crash

我的应用是插入/更新/删除/显示数据库应用。到目前为止,我已经成功设法开发了Insert部分,但是在尝试显示数据库内容时我的应用程序崩溃了,尽管它已成功构建。

我有一个名为DatabaseHelper的独立活动,另一个是应该在ActionDialog中显示数据的活动。

当我点击"显示数据"它崩溃了我的MainActivity中的按钮

DatabaseHelper.java

public DatabaseHelper (Context context) {
    super (context, DATABASE_NAME, null, 1);
    //SQLiteDatabase db = this.getWritableDatabase();
}

...

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
    return res;
}

和Activity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pregled);

    buttonPregled = (Button)findViewById(R.id.BtnPregled);
    viewAll();
}

//
public void viewAll(){
    buttonPregled.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Cursor res = myDb.getAllData();
            if(res.getCount() == 0){
                //show message
                showMessage("Error", "Table empty");
                return;
            }

            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()){
                buffer.append("ID:"+ res.getString(0)+"\n");
                buffer.append("Naslov:"+ res.getString(1)+"\n");
                buffer.append("Datum:"+ res.getString(2)+"\n");
                buffer.append("Kontakt osoba:"+ res.getString(3)+"\n");
                buffer.append("Kontakt broj:"+ res.getString(4)+"\n");
                buffer.append("Lokacija:"+ res.getString(5)+"\n");
                buffer.append("Pogodba:"+ res.getString(6)+"\n");
                buffer.append("Zarada:"+ res.getString(7)+"\n\n");
            }

            //Show all data
            showMessage("Svirke", buffer.toString());
        }
    });
}

public void showMessage (String title, String Message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();
}

堆栈跟踪

05-09 15:20:07.935 4137-4137/com.example.vramba.evis17 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.vramba.evis17, PID: 4137
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vramba.evis17/com.example.vramba.evis17.PregledActivity}: 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:2702)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5951)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
                                                                      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.example.vramba.evis17.PregledActivity.viewAll(PregledActivity.java:25)
                                                                         at com.example.vramba.evis17.PregledActivity.onCreate(PregledActivity.java:20)
                                                                         at android.app.Activity.performCreate(Activity.java:6289)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5951) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

1 个答案:

答案 0 :(得分:0)

试试这个:

Cursor res = myDb.getAllData();
res.moveToFirst(); // OR you can also write res.moveToPosition(0);
if(res.getCount() == 0)
{
   //show message
   showMessage("Error", "Table empty");
   return;
}

如果您的数据库已正确初始化,那么此操作将在此之前初始化您的数据库。