单击确认按钮后,应用程序停止并重新启动

时间:2014-05-01 10:46:39

标签: android android-alertdialog

我正在Eclipse上进行Android开发。该应用程序可以执行以下操作:

  • 添加新作业
  • 修改作业
  • 删除作业

现在我遇到的一个问题是,每当我尝试删除一项任务时,点击确认按钮后应用程序就会崩溃。

这是logcat:

05-01 06:31:26.772: I/Choreographer(1417): Skipped 36 frames!  The application may be doing too much work on its main thread.
05-01 06:31:26.912: I/Choreographer(1417): Skipped 30 frames!  The application may be doing too much work on its main thread.
05-01 06:31:37.582: I/Choreographer(1417): Skipped 103 frames!  The application may be doing too much work on its main thread.
05-01 06:31:37.772: I/Choreographer(1417): Skipped 39 frames!  The application may be doing too much work on its main thread.
05-01 06:31:40.232: I/Choreographer(1417): Skipped 58 frames!  The application may be doing too much work on its main thread.
05-01 06:31:40.682: I/Choreographer(1417): Skipped 112 frames!  The application may be doing too much work on its main thread.
05-01 06:31:42.662: D/AndroidRuntime(1417): Shutting down VM
05-01 06:31:42.662: W/dalvikvm(1417): threadid=1: thread exiting with uncaught exception (group=0xb3adeba8)
05-01 06:31:42.682: E/AndroidRuntime(1417): FATAL EXCEPTION: main
05-01 06:31:42.682: E/AndroidRuntime(1417): Process: com.mada.assignmentmanager, PID: 1417
05-01 06:31:42.682: E/AndroidRuntime(1417): java.lang.NullPointerException
05-01 06:31:42.682: E/AndroidRuntime(1417):     at com.mada.assignmentmanager.AssignmentManagerDatabase.deleteAssignment(AssignmentManagerDatabase.java:91)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at com.mada.assignmentmanager.AssignmentManagerView$1.onClick(AssignmentManagerView.java:74)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at android.os.Looper.loop(Looper.java:136)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at java.lang.reflect.Method.invokeNative(Native Method)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at java.lang.reflect.Method.invoke(Method.java:515)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-01 06:31:42.682: E/AndroidRuntime(1417):     at dalvik.system.NativeStart.main(Native Method)
05-01 06:31:42.722: D/dalvikvm(1417): GC_FOR_ALLOC freed 166K, 7% free 3914K/4196K, paused 33ms, total 41ms
05-01 06:31:47.922: I/Process(1417): Sending signal. PID: 1417 SIG: 9
05-01 06:31:49.402: D/dalvikvm(1452): GC_FOR_ALLOC freed 73K, 7% free 2947K/3140K, paused 125ms, total 129ms
05-01 06:31:49.402: I/dalvikvm-heap(1452): Grow heap (frag case) to 3.596MB for 635812-byte allocation
05-01 06:31:49.462: D/dalvikvm(1452): GC_FOR_ALLOC freed 2K, 6% free 3565K/3764K, paused 52ms, total 52ms
05-01 06:31:50.292: I/Choreographer(1452): Skipped 59 frames!  The application may be doing too much work on its main thread.
05-01 06:31:50.322: D/gralloc_goldfish(1452): Emulator without GPU emulation detected.

这是关于“删除分配”操作的代码:

private void deleteAssignment() {
    // TODO Auto-generated method stub
    AlertDialog.Builder dgbox = new AlertDialog.Builder(this);
    dgbox.setTitle("Confirm to delete");
    dgbox.setMessage("Are you sure to delete this assignment?");

    dgbox.setPositiveButton("Delete",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                    final AssignmentManagerDatabase db = new AssignmentManagerDatabase(
                            AssignmentManagerView.this);
                    db.deleteAssignment(assignID);
                    finish();
                }
            }); // setPositiveButton

    // provide a button that simply dismisses the dialog
    dgbox.setNegativeButton("Cancel", null);
    dgbox.show();
}

从上面的代码中,你可以看到我在选择删除作业时创建了一个对话框。应用程序将停止并重新启动。所选作业不会被删除。

我有一个菜单来选择删除:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.menuAdd:
        Intent addAssignment = new Intent(this, AssignmentAddNew.class);
        startActivity(addAssignment);
        return true;
    case R.id.menuModify:
        Intent editAssignment = new Intent(this, AssignmentEdit.class);
        startActivity(editAssignment);
        return true;
    case R.id.menuDelete:
        deleteAssignment();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

以下是完整的数据库代码:

package com.mada.assignmentmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class AssignmentManagerDatabase {

public static final String KEY_ROWID = "_id";
public static final String KEY_MODULECODE = "module_code";
public static final String KEY_ASSIGNMENTNAME = "assignment_name";
public static final String KEY_MARKSPROPORTION = "marks_proportion";
public static final String KEY_DUEDATE = "due_date";
public static final String KEY_PROGRESS = "_progress";

private static final String DATABASE_NAME = "Assignmentdb";
private static final String DATABASE_TABLE = "AssignmentTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
String cmd;

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    private static final String CREATE_NEW_DATABASE = "CREATE TABLE "
            + DATABASE_TABLE + " (" + KEY_ROWID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_MODULECODE
            + " TEXT  NOT NULL, " + KEY_ASSIGNMENTNAME + " TEXT NOT NULL, "
            + KEY_MARKSPROPORTION + " INTEGER, " + KEY_DUEDATE
            + " TEXT NOT NULL, " + KEY_PROGRESS + " INTEGER);";

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_NEW_DATABASE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("AssignmentDBAdapter", "Upgrading from version" + oldVersion
                + " to " + newVersion
                + ", which will destroy all the old data.");
        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
        onCreate(db);
    }
}

public AssignmentManagerDatabase(Context c) {
    ourContext = c;
}

public AssignmentManagerDatabase open() {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public void createEntry(String moduleCode, String assignmentName,
        String marksProportion, String dueDate, String assignmentProgress) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_MODULECODE, moduleCode);
    cv.put(KEY_ASSIGNMENTNAME, assignmentName);
    cv.put(KEY_MARKSPROPORTION, marksProportion);
    cv.put(KEY_DUEDATE, dueDate);
    cv.put(KEY_PROGRESS, assignmentProgress);
    ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public Cursor getAllAssignments() {
    // String[] columns = new String[]{KEY_ROWID, KEY_MODULECODE,
    // KEY_ASSIGNMENTNAME, KEY_MARKSPROPORTION, KEY_DUEDATE, KEY_PROGRESS};
    String[] columns = new String[] { KEY_ROWID, KEY_DUEDATE };
    return ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
            null, KEY_DUEDATE);
}

public void deleteAssignment(long id) {
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + id, null);
}

public Cursor getOneAssignment(long assignID) {
    // TODO Auto-generated method stub
    return ourDatabase.query(DATABASE_TABLE, null, KEY_ROWID + "="
            + assignID, null, null, null, null);
}

}

这是我的数据库上的删除操作:

    public void deleteAssignment(long id) {
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + id, null);
}

2 个答案:

答案 0 :(得分:0)

final Context context = this;
                dgbox.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                    // set title
                    dgbox.setTitle("");

                    // set dialog message
                    dgbox
                        .setMessage("No Records Found!")
                        .setCancelable(false)
                        .setPositiveButton("Exit",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                    final AssignmentManagerDatabase db = new AssignmentManagerDatabase(
                    AssignmentManagerView.this);
                    db.deleteAssignment(assignID);
                    finish();

                            }
                          });

                        // create alert dialog
                        alertDialog = dgbox.create();

                        // show it
                        alertDialog.show()

答案 1 :(得分:0)

在调用deleteAsignment

之前,需要调用open()方法来打开数据库

没有数据库打开调用,我们的数据库将为null

        final AssignmentManagerDatabase db = new AssignmentManagerDatabase(
                        AssignmentManagerView.this);
        db.open():
                db.deleteAssignment(assignID);