1

每次我运行我的代码时,我都会在我的 logcat 中得到这个,并且无法弄清楚如何修复它。当我更新时出现此错误,我在删除时使用类似的代码并且效果很好。我在更新中使用了断点,它一直持续到函数的末尾。我以为在 startActivityForResult 会转到下一页,但事实并非如此。startActivityForResult 完全相同的代码在另一个函数中工作

这是更新的代码

    public void edit_to_report_page(final View v) {

    LayoutInflater inflater = LayoutInflater.from(this);
    View addView = inflater.inflate(R.layout.new_vehicle, null);
    idNum = (EditText) addView.findViewById(R.id.deleteRecord);
    new AlertDialog.Builder(this)
            .setTitle("Edit a report")
            .setMessage("Enter the Id number of the report to edit")
            .setView(addView)
            .setPositiveButton("Edit",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            String id = idNum.getText().toString();
                            uniqueID = Long.parseLong(id);
                            newID(uniqueID);
                            info.open();

                            try {
                                boolean correctNumber = info
                                        .Exists(uniqueID);
                                if (!correctNumber) {
                                    Toast.makeText(getApplicationContext(),
                                            "Number not found",
                                            Toast.LENGTH_SHORT).show();

                                    info.close();

                                }else{

                                    info.close();
                                    intent = new Intent(v.getContext(),
                                            report.class);
                                    startActivityForResult(intent, 0);
                                }
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        }
                    }).show();

}

这是有效的删除代码

public void delete_db(View v) {

    LayoutInflater inflater = LayoutInflater.from(this);
    View addView = inflater.inflate(R.layout.new_vehicle, null);
    idNum = (EditText) addView.findViewById(R.id.deleteRecord);
    final Dialog d = new Dialog(this);
    final TextView tv = new TextView(this);

    new AlertDialog.Builder(this)
            .setTitle("Delete a report")
            .setMessage("Enter the Id number of the report to delete")
            .setView(addView)
            .setPositiveButton("Delete",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            String id = idNum.getText().toString();
                            long primaryId = Long.parseLong(id);
                            info.open();
                            try {
                                boolean inCorrectNumber = info
                                        .deleteInspection(primaryId);
                                if (inCorrectNumber) {
                                    Toast.makeText(getApplicationContext(),
                                            "Number not found",
                                            Toast.LENGTH_SHORT).show();

                                }
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            info.close();
                            dbInfo();
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                        }
                    }).show();
}

这是我的 dbAdapter 中用于删除和存在(更新)的代码

public boolean Exists(long _id)throws Exception
{

    Cursor c = db.query(DB_TABLE, new String[] { KEY_ROWID},
            "_id=" + _id, null, null, null, null);
     boolean exists=(c.getCount()>0);
    c.close();
    return exists;
}

public boolean deleteInspection(long _id)throws Exception
{
    int numResults = 0;
    numResults = db.delete(DB_TABLE,"_id=" + _id , null);
    return (numResults < 1);
}

这是我在 logcat 中得到的

04-10 09:47:05.006: E/CursorWindow(2961): 对字段槽 0,-1 的错误请求。numRows = 1,numColumns = 1
04-10 09:47:05.016: D/AndroidRuntime(2961): 关闭 VM
04-10 09:47:05.027: W/dalvikvm(2961): threadid=1: 线程以未捕获的异常退出 (group=0x4001d800)
04-10 09:47:05.046:E/AndroidRuntime(2961):致命异常:主要
04-10 09:47:05.046: E/AndroidRuntime(2961): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.signalsetapp/com.signalsetapp.inspectionchecklist.report}: java.lang.IllegalStateException: 获取字段槽从第 0 行 col -1 失败
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.os.Handler.dispatchMessage(Handler.java:99)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.os.Looper.loop(Looper.java:123)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.ActivityThread.main(ActivityThread.java:4627)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 java.lang.reflect.Method.invokeNative(Native Method)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 java.lang.reflect.Method.invoke(Method.java:521)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-10 09:47:05.046: E/AndroidRuntime(2961): at dalvik.system.NativeStart.main(Native Method)
04-10 09:47:05.046: E/AndroidRuntime(2961): 由: java.lang.IllegalStateException: 从行 0 col -1 获取字段槽失败
04-10 09:47:05.046: E/AndroidRuntime(2961): at android.database.CursorWindow.getString_native(Native Method)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.database.CursorWindow.getString(CursorWindow.java:329)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
04-10 09:47:05.046: E/AndroidRuntime(2961): at com.signalsetapp.inspectionchecklist.report.getInfoFromDB(report.java:129)
04-10 09:47:05.046: E/AndroidRuntime(2961): at com.signalsetapp.inspectionchecklist.report.onCreate(report.java:111)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-10 09:47:05.046: E/AndroidRuntime(2961): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-10 09:47:05.046: E/AndroidRuntime(2961): ... 11 更多
4

2 回答 2

2

很高兴提供帮助,添加以便您可以标记为答案:)

看起来“report.java:129”是这里的坏人,是你的代码吗?如果是这样,您能否在上面的内联代码中标记它。应该有助于调试这个:D – erbsman 4 小时前

于 2012-04-10T21:47:56.120 回答
1

亚伦我有同样的异常,解决方案是Check your column name you want to access it from cursor is same name as exists in database。并且还删除white space并首先尝试查询SQLITE browser然后尝试编码并Debugging ,I mean print query in logcat and copy ,and paste and sqlite Browser为你做一些关键,否则需要更多的时间和困难来解决它!

于 2012-04-10T17:41:38.063 回答