Android 运行自定义游标适配器时出错,参数异常非法

Android 运行自定义游标适配器时出错,参数异常非法,android,sqlite,logcat,Android,Sqlite,Logcat,我已经用这些更改更新了我的帖子,我插入了“\u id”,一切正常运行 我希望有人能帮我解决这个问题,因为我对android和java还很陌生。我已经创建了一个使用SQLite数据库的应用程序,我正在使用一个自定义光标适配器打印一行数据库以及两个按钮和一个editText。我想我有正确的代码,但当我尝试运行应用程序时,我得到了一个IllegalArgumentException,我已经在这个论坛上浏览了几天了,我仍然非常困惑。如果有人能指出我的错误并帮我改正,那就太好了 这是我的主要活动 impo

我已经用这些更改更新了我的帖子,我插入了“\u id”,一切正常运行

我希望有人能帮我解决这个问题,因为我对android和java还很陌生。我已经创建了一个使用SQLite数据库的应用程序,我正在使用一个自定义光标适配器打印一行数据库以及两个按钮和一个editText。我想我有正确的代码,但当我尝试运行应用程序时,我得到了一个IllegalArgumentException,我已经在这个论坛上浏览了几天了,我仍然非常困惑。如果有人能指出我的错误并帮我改正,那就太好了

这是我的主要活动

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.pinchtapzoom.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MyActivity extends Activity {

private CustomCursorAdapter customAdapter;
//private com.example.rory.dbtest.DBAdapter databaseHelper;
public ListView list1;

com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
//CustomCursorAdapter c = new CustomCursorAdapter(this,c);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    list1 = (ListView)findViewById(R.id.data_list);
    db.open();


    Button addBtn = (Button)findViewById(R.id.add);
    addBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyActivity.this, addassignment.class);
            startActivity(i);
        }
    });

    Button deleteBtn = (Button)findViewById(R.id.delete);
    deleteBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyActivity.this, Delete.class);
            startActivity(i);
        }
    });

    Button updateBtn = (Button)findViewById(R.id.update);
    updateBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MyActivity.this, Update.class);
            startActivity(i);
        }
    });


    try {
        String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
        File f = new File(destPath);
        if (!f.exists()) {
            CopyDB( getBaseContext().getAssets().open("mydb"),
                    new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


   new Handler().post(new Runnable() {
        @Override
        public void run() {
            //Log.d("test", "customadapter is " + customAdapter.toString());
            //Log.d("test", "databaseHelper is " + databaseHelper.toString());
            customAdapter = new CustomCursorAdapter(MyActivity.this, db.getAllRecords());
            list1.setAdapter(customAdapter);
        }
    });


}

private class DBAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    //private ArrayList<>

    @Override
    public int getCount() {

        return 0;
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        return null;
    }

}

public void CopyDB(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    //---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}
}
这是我的自定义游标类 导入android.content.Context; 导入android.database.Cursor; 导入android.view.LayoutInflater; 导入android.view.view; 导入android.view.ViewGroup; 导入android.widget.CursorAdapter; 导入android.widget.TextView; 导入com.pinchtapzoom.R

public class CustomCursorAdapter extends CursorAdapter {

public CustomCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // when the view will be created for first time,
    // we need to tell the adapters, how each item will look
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.row, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    // here we are setting our data
    // that means, take the data from the cursor and put it in views

    TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

}
}
这是我运行应用程序时遇到的logcat错误

    4501-4501/com.example.rory.dbtest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.rory.dbtest, PID: 4501
    java.lang.IllegalArgumentException: column '_id' does not exist
        at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
        at android.widget.CursorAdapter.init(CursorAdapter.java:172)
        at android.widget.CursorAdapter.<init>(CursorAdapter.java:120)
        at com.example.rory.dbtest.CustomCursorAdapter.<init>(CustomCursorAdapter.java:19)
        at com.example.rory.dbtest.MyActivity$4.run(MyActivity.java:94)
4501-4501/com.example.rory.dbtest E/AndroidRuntime﹕ 致命异常:主
进程:com.example.rory.dbtest,PID:4501
java.lang.IllegalArgumentException:列“\u id”不存在
位于android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
位于android.widget.CursorAdapter.init(CursorAdapter.java:172)
位于android.widget.CursorAdapter.(CursorAdapter.java:120)
位于com.example.rory.dbtest.CustomCursorAdapter。(CustomCursorAdapter.java:19)
位于com.example.rory.dbtest.MyActivity$4.run(MyActivity.java:94)

CursorAdapter总是需要一个名为
\u id
的列,您必须为您的CursorAdapter提供它,将表列id更改为
\u id
,或者当您从表中查询时,将
id
列的别名设置为
\u id
,正如卢卡斯指出的那样,更改定义以使
id
成为
\u id

public static final String KEY_ROWID = "_id";


基本上,您需要做的是将id列更改为_id,因为在使用自定义游标适配器时,它需要列_id。使用带有自定义游标适配器的数据库是一条不成文的规则

应使用列名“\u id”,并且“id”表具有,请将create table查询更改为具有“\u id”很酷谢谢你的回复我会尝试一下我需要将数据库中的列名从'id'更改为'u id'?或者你可以从你的id列中创建别名我尝试运行你所做的更改,但仍然不起作用,它说没有这样的列?
public static final String KEY_ROWID = "_id";
private static final String DATABASE_CREATE =
    "create table if not exists assignments (_id integer primary key autoincrement, "
            + "item VARCHAR not null, litres date );";