当前位置: 首页 > 面试题库 >

无法从CursorWindow读取行0,col -1?

卫博
2023-03-14
问题内容

使用数据库时出现问题。当我运行SQLView.java时,出现一个致命异常:

java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.jacob.eindproject/com.jacob.eindproject.SQLView}:     java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure     the Cursor is initialized correctly before accessing data from it.

希望有人可以帮助我。我将发布所有与数据库相关的代码。当您需要更多信息时,请告诉我,不要犹豫,索取更多信息!

我认为我的专栏有问题,无法找出原因。

另外,我已经阅读了该网站上的所有相关问题,但是我找不到应该在何处声明我的列以及要添加/更改的内容。有人知道怎么做吗?:)

首先:我的数据库类:

    package com.jacob.eindproject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import java.sql.*;

public class Database {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "persons_name";
    public static final String KEY_HOTNESS = "persons_hotness";

    private static final String DATABASE_NAME = "Databasedb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;

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

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override //I create it here, right?
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_NAME + " TEXT NOT NULL, " +
                    KEY_HOTNESS + " TEXT NOT NULL);"                    
        );


        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }

        public void close(Database database) {
            // TODO Auto-generated method stub

        }
    }



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

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

    public void close() {

    ourHelper.close(); 
}



    public long createEntry(String name, String hotness) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_HOTNESS, hotness);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(DATABASE_NAME);
        int iHotness = c.getColumnIndex(KEY_HOTNESS);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iHotness) + "\n";


        }

        return result;
    }
}

SQLite类,用于修复输入:

package com.jacob.eindproject;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View.OnClickListener;


public class SQLite extends Activity implements View.OnClickListener {

    Button sqlUpdate, sqlView;
    EditText sqlName, sqlHotness;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqllite);
        sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
        sqlName = (EditText) findViewById(R.id.etSQLName);
        sqlHotness = (EditText) findViewById(R.id.etSQLHotness);

        sqlView = (Button) findViewById(R.id.bSQLopenView);
        sqlView.setOnClickListener((android.view.View.OnClickListener) this);
        sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.bSQLUpdate:


            boolean didItWork = true;
            try{            
            String name = sqlName.getText().toString();
            String hotness = sqlHotness.getText().toString();

            Database entry = new Database(SQLite.this);
            entry.open();
            entry.createEntry(name, hotness);
            entry.close();

            }catch (Exception e ){
                didItWork = false;

            }finally{
                if (didItWork){
                    Dialog d = new Dialog(this);
                    d.setTitle("Heak Yeay");
                    TextView tv = new TextView(this);
                    tv.setText("Succes");
                    d.setContentView(tv);
                    d.show();
            }
        }

            break;
        case R.id.bSQLopenView:
            Intent i = new Intent("com.jacob.eindproject.SQLVIEW");
            startActivity(i);


        }
        }

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

    }

    }

之后,我的SQLView类查看来自SQLite类的输入:

package com.jacob.eindproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SQLView extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlview);
        TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
        Database info = new Database(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);





    }

}

现在,我的xml文件:

SQLView.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">


    <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tableLayout1">

        <TableRow>

                <TextView android:text="@string/Names" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/>

                <TextView android:text="@string/Hotness" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" />

        </TableRow>


    </TableLayout>

        <TextView android:id="@+id/tvSQLinfo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/info"/>

</LinearLayout>

sqllite.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


    <TextView
        android:id="@+id/Naam" 
        android:text="@string/Naam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/etSQLName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </EditText>

    <TextView
        android:id="@+id/hotness" 
        android:text="@string/hotnessscale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

    <EditText
        android:id="@+id/etSQLHotness"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </EditText>

    <Button
        android:id="@+id/bSQLUpdate"
        android:text="@string/Update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" ></Button>



    <Button
        android:id="@+id/bSQLopenView"
        android:text="@string/View"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
    ></Button>

</LinearLayout>

用于修复菜单的Menu类,带有一个OnListItemClick(那里还有其他活动,例如Overgewicht。我猜这些都不要介意。):

package com.jacob.eindproject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity implements OnItemClickListener {


    String classes[] = { "BMI- Calculator", "Ondergewicht", "Gezond Gewicht", "Overgewicht", "Database", "Bekijk Database"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    //Positie 0 is het eerste item (dus de BMI-Calculator.)
    super.onListItemClick(l, v, position, id);

    switch(position)
    {
    case 0: 
    Intent openStartingPoint = new Intent(getApplicationContext(),MainActivity.class);
    startActivity(openStartingPoint);   
    break;

    case 1: 
    Intent openOndergewicht = new Intent(getApplicationContext(),Ondergewicht.class);
    startActivity(openOndergewicht);
    break;

    case 2: 
    Intent openGezondgewicht = new Intent(getApplicationContext(),Gezond_gewicht.class);
    startActivity(openGezondgewicht);   
    break;

    case 3: 
    Intent openOvergewicht = new Intent(getApplicationContext(),Overgewicht.class);
    startActivity(openOvergewicht);

    break;


    case 4: 
    Intent openDatabase = new Intent(getApplicationContext(),SQLite.class);
    startActivity(openDatabase);

    break;

    case 5: 
    Intent openViewdatabase = new Intent(getApplicationContext(),SQLView.class);
    startActivity(openViewdatabase);

    break;

    }


    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
        // TODO Auto-generated method stub

    }
}

我的Android清单文件:(再次,我声明了其他一些东西,不要介意这些!)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jacob.eindproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >


        <activity
            android:name="com.jacob.eindproject.Menu"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.jacob.eindproject.Inleiding"
            android:label="@string/app_name" >
                    <intent-filter>
                            <action android:name="android.intent.action.MAIN" />
                            <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
        </activity>

        <activity
            android:name="com.jacob.eindproject.MainActivity"
            android:label="@string/app_name" >
                     <intent-filter>
                            <action android:name="android.intent.action.MAIN" />
                            <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
        </activity>

        <activity
            android:name="com.jacob.eindproject.Ondergewicht"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.jacob.eindproject.Gezond_gewicht"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.jacob.eindproject.Overgewicht"
            android:label="@string/app_name" >
        </activity>

        <activity
            android:name="com.jacob.eindproject.Database"
            android:label="@string/app_name" >
                    <intent-filter>
                            <action android:name="com.jacob.eindproject.DATABASE" />
                            <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
        </activity>

        <activity
            android:name=".SQLView"
            android:label="@string/app_name" >
                    <intent-filter>
                            <action android:name="com.jacob.eindproject.SQLVIEW" />
                            <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>

        </activity>


        <activity
            android:name="com.jacob.eindproject.SQLite"
            android:label="@string/app_name" >
                    <intent-filter>
                            <action android:name="com.jacob.eindproject.SQLITE" />
                            <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
        </activity>



    </application>

</manifest>

最后,我的LogCat向您显示错误:

12-14 11:04:18.227: E/AndroidRuntime(1577): FATAL EXCEPTION: main
12-14 11:04:18.227: E/AndroidRuntime(1577): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jacob.eindproject/com.jacob.eindproject.SQLView}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.os.Looper.loop(Looper.java:137)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at java.lang.reflect.Method.invoke(Method.java:525)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at dalvik.system.NativeStart.main(Native Method)
12-14 11:04:18.227: E/AndroidRuntime(1577): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.database.CursorWindow.nativeGetString(Native Method)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.database.CursorWindow.getString(CursorWindow.java:434)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.jacob.eindproject.Database.getData(Database.java:95)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.jacob.eindproject.SQLView.onCreate(SQLView.java:16)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.Activity.performCreate(Activity.java:5133)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-14 11:04:18.227: E/AndroidRuntime(1577):     ... 11 more

希望有人花时间帮助我。我非常感谢那些对此感兴趣的人。提前谢谢大家。

雅各布


问题答案:
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(DATABASE_NAME);

您的 光标 没有名称为的
DATABASE_NAME。您可能打算KEY_NAME为此getColumnIndex()调用以这种形式返回-1,并且尝试使用此类索引获取数据将导致您所看到的异常。



 类似资料:
  • 问题内容: 我正在创建一个使用SQLiteDatabase来存储项目的应用程序。但是,从数据库中填充数据时出现错误,应用崩溃。 这是我的数据库- Note.java- NotesDatabase.java- 这是LogCat错误日志- 请帮助我找出我的代码出了什么问题。提前致谢!! 问题答案: 您的主题行具有误导性。您的堆栈跟踪中的异常说 这实际上意味着无法在中找到指定的列,并且-1作为列索引返回

  • 我正在开发一个应用程序,下载一些文件,并将其文本保存在文件内容字段中的数据库中。文件大小可以从一些KB到10 MB不等。该应用程序在保存时适用于所有尺寸。在长文件内容记录上使用select语句时出现问题。它给 java.lang.非法状态异常:无法从CursorWindow读取行0, col0 获取这样的行时。字段内容大小是否有限制?如果是这样,那么为什么它让我们在检索时保存并给出错误呢?这是我截

  • 我尝试使用上下文菜单从listview中删除数据,得到了java.lang.IllegalStateException:Could not read row 0,col 2 from CursorWindow。在从游标访问数据之前,请确保游标已正确初始化。 第一行错误AssignmentRecord.SetAssid(Cursor.GetString(2)); 第二个错误是List records

  • 我想分享 /Internal存储/aaa/bbb.dat文件到另一个应用程序通过FileProvider。但它出错了。 我已经将路径放入了provider\u path。xml并将提供程序放入清单文件中。 我希望gmail或midrop/shareme能够打开和bbb。dat文件附呈。但它出错了 显示 提供者路径。xml 我是根据这些帖子编写代码的 Android:自定义外部存储文件夹上的文件提供

  • 我已经创建了一个非常简单的数据库android应用程序。它接受输入并显示结果。添加按钮用于添加输入,删除按钮用于删除存储在SQLite数据库中的输入。我在Android SQLite中的光标指向正确,但仍然面临错误:由:java.lang.IllegalStateException引起:无法从CursorWindow读取第0,col-1行。在从中访问数据之前,请确保光标已正确初始化。以下是游标部分