【问题标题】:Error with inserting data into sqlite database将数据插入 sqlite 数据库时出错
【发布时间】:2010-12-02 08:39:10
【问题描述】:

我正在尝试使用对话框包装器将数据插入 SqlLite 数据库。但是,我无法这样做。我有 2 个名为 Title 和 Template 的文本要插入到数据库中。但它崩溃了。 下面是我的java代码:
@Override 公共布尔 onCreateOptionsMenu(菜单菜单) { menu.add(0,MENU_ITEM_INSERT,0,R.string.menu_insert); 返回真; }

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case MENU_ITEM_INSERT:
            insert();
            //startActivity(new Intent(Intent.ACTION_INSERT,getIntent().getData()));
            return true;
    }
    return(super.onOptionsItemSelected(item));
}

@Override 
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo)
{
    AdapterView.AdapterContextMenuInfo info;
    try {
         info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
        return;
    }

    mCursor = (Cursor) getListAdapter().getItem(info.position);

    menu.setHeaderTitle(mCursor.getString(COLUMN_INDEX_TITLE));

    menu.add(0,MENU_ITEM_DELETE,0 ,R.string.menu_delete);
    menu.add(1,MENU_ITEM_ADDTMESSAGE,1,R.string.menu_add);
}

@Override
public boolean onContextItemSelected(MenuItem item)
{
    AdapterView.AdapterContextMenuInfo info;
    try {
         info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
        return false;
    }

    switch(item.getItemId())
    {
        case MENU_ITEM_DELETE:
            //delete selected row
            delete(info.id);
            return true;
        case MENU_ITEM_ADDTMESSAGE:
            //ADD TO MESSAGE
            return true;
    }
    return(super.onContextItemSelected(item));
}

private void insert()
{
    LayoutInflater inflater = LayoutInflater.from(this);
    View addView = inflater.inflate(R.layout.templates_editor,null);
    final DialogWrapper wrapper = new DialogWrapper(addView);

    new AlertDialog.Builder(this)
    .setTitle(R.string.menu_insert)
    .setView(addView)
    .setPositiveButton(R.string.item_ok,new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            processInsert(wrapper);
        }
    })
    .setNegativeButton(R.string.item_cancel,new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // ignore, just dismiss
        }
    })
    .show();
}

private void delete(final long rowId)
{
    if(rowId > 0)
    {
        new AlertDialog.Builder(this)
                        .setTitle(R.string.menu_delete)
                        .setPositiveButton(R.string.item_ok, 
                                new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                processDelete(rowId);
                            }
                        })
                        .setNegativeButton(R.string.item_cancel,
                                new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int which) {
                                        //dismiss dialog
                                    }
                                }).show();
    }
}

private void processDelete(long rowId)
{
    String[] args = {String.valueOf(rowId)};

    db.getWritableDatabase().delete("Templates", Templates._ID, args);
    mCursor.requery();
}

private void processInsert(DialogWrapper wrapper)
{
    ContentValues cv = new ContentValues();

    cv.put("title", wrapper.getTitle());
    cv.put("template", wrapper.getTemplate());

    db.getWritableDatabase().insert("Templates", "title", cv);
    db.getWritableDatabase().insert("Templates", "template", cv);
    mCursor.requery();
}

class DialogWrapper{
    EditText titleField = null;
    EditText templateField = null;
    View base = null;

    DialogWrapper(View base) {
        this.base = base;
    }

    String getTitle(){
        return(getTitleField().getText().toString());
    }

    String getTemplate(){
        return(getTemplateField().getText().toString());            
    }

    private EditText getTitleField(){
        if(titleField == null){
            titleField = (EditText) findViewById(R.id.title);
        }
        return titleField;
    }

    private EditText getTemplateField(){
        if(templateField == null){
            templateField = (EditText) findViewById(R.id.template);
        }
        return templateField;
    }
}

下面是我的 logCat:

12-02 08:23:17.712:错误/AndroidRuntime(204):未捕获的处理程序:线程主因未捕获的异常而退出
12-02 08:23:17.862: 错误/AndroidRuntime(204): java.lang.NullPointerException
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList$DialogWrapper.getTitle(TemplatesList.java:214)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList.processInsert(TemplatesList.java:196)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList.access$0(TemplatesList.java:192)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList$1.onClick(TemplatesList.java:150)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 android.os.Handler.dispatchMessage(Handler.java:99)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 android.os.Looper.loop(Looper.java:123)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 android.app.ActivityThread.main(ActivityThread.java:4363)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 java.lang.reflect.Method.invokeNative(Native Method)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 java.lang.reflect.Method.invoke(Method.java:521)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-02 08:23:17.862: 错误/AndroidRuntime(204): at dalvik.system.NativeStart.main(Native Method)

编辑
这是我要求的与我的评论有关的堆栈跟踪:

12-06 16:54:12.466:INFO/ActivityManager(58):开始活动:Intent { cmp=joel.GroupSMS/.TemplateEdit }
12-06 16:54:16.550:调试/AndroidRuntime(254):关闭 VM 12-06 16:54:16.550: WARN/dalvikvm(254): threadid=3: 线程以未捕获的异常退出 (group=0x4001b188)
12-06 16:54:16.550:错误/AndroidRuntime(254):未捕获的处理程序:线程主因未捕获的异常而退出
12-06 16:54:16.736: 错误/AndroidRuntime(254): java.lang.RuntimeException: 无法启动活动 ComponentInfo{joel.GroupSMS/joel.GroupSMS.TemplateEdit}: java.lang.NullPointerException
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.os.Looper.loop(Looper.java:123)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.main(ActivityThread.java:4363)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 java.lang.reflect.Method.invoke(Method.java:521)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-06 16:54:16.736: 错误/AndroidRuntime(254): at dalvik.system.NativeStart.main(Native Method)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 由: java.lang.NullPointerException
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.content.ContentResolver.acquireProvider(ContentResolver.java:754)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.content.ContentResolver.query(ContentResolver.java:197)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.Activity.managedQuery(Activity.java:1495)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 joel.GroupSMS.TemplateEdit.onCreate(TemplateEdit.java:77)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

【问题讨论】:

    标签: android database


    【解决方案1】:

    如果代码中没有行号来匹配回溯,很难判断,但我猜(EditText) findViewById(R.id.title) 正在返回 null。在您进行插入时检查您的应用在层次结构查看器中,并确保您认为在层次结构中存在的视图在您处理插入代码时确实存在。

    【讨论】:

    • 你已经很接近了。错误行位于“getTitleField”。是的,在处理插入代码时,视图位于层次结构中。我想。
    • 我不知道你为什么要打扰 DialogWrapper;这不是列表情况下的视图回收(通过 ID 查找视图时 10% 左右的性能值得缓存);当您需要它时,您可能应该只是通过 ID 获取 editText 的值,而不是使用中间对象。但无论如何,请尝试使用 dialog.findViewById(whatever) 而不仅仅是 Activity 的 findViewById(whatever),将对话框(而不是包装器)传递给您的方法。
    • DialogWrapper 仅用于测试。如果可能的话,我宁愿将它链接到另一个布局并从数据库中检索数据并插入到 EditTexts 中。
    • 如何在不使用内容提供程序(如代码)的情况下删除/插入/更新数据库中的数据?能给我一个例子吗?我必须使用URI吗?希望你能帮助我。
    • mCursor = managedQuery(mUri, PROJECTION,null,null,null); PROJECTION 有 3 个数组项。看来我在这一行有一个空指针异常。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2012-10-30
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多