从Cursor中获取值但应用程序崩溃

时间:2015-05-30 06:20:26

标签: android sqlite

当我点击视图按钮时我试图从数据库中获取值,这是另一个导航活动,当我点击查看全部按钮时触发了CustodianViewActivity以在列表视图中显示数据库中的所有值但是在我的情况下,每次我尝试从数据库中查看数据时应用程序崩溃。我不确定我哪里出错了。

数据库类

    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 DBHandler extends SQLiteOpenHelper   {
         private static DBHandler instance;
        // Database Name
        public static final String DATABASE_NAME ="AssetDB.db";

        //Database version
        public static final int  Databasevr = 1;

        //Custodian Table Name
        public static final String TABLE_CUSTODIAN = " Custodian";
        // Columbs in the Custodian Table

        public static final String CUSTODIAN_ID = "_CustID";
        public static final String CUSTODIAN_NAME = "CustName";
        public static final String CUSTODIAN_DESIGNATION = "CustDesign";
        public static final String CUSTODIAN_DEPARTMENT = "CustDepart";private static final String CREATE_TABLE_CUSTODIAN = "CREATE TABLE" + TABLE_CUSTODIAN  + "("
            + CUSTODIAN_ID  + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
            + CUSTODIAN_NAME +  " TEXT NOT NULL,"
            + CUSTODIAN_DESIGNATION +   " TEXT NOT NULL,"
            + CUSTODIAN_DEPARTMENT + " TEXT NOT NULL" + ");";
// constructor passing parameter passing Database name and Database version
     public DBHandler(Context ct)
     {
         super(ct, DATABASE_NAME, null, Databasevr);
     }



        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            // Creating the tables
            db.execSQL(CREATE_TABLE_CUSTODIAN);
            db.execSQL(CREATE_TABLE_ASSET);
            Log.d("Tables","Tables have been created");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

            // dropping the tables
            db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_CUSTODIAN );
            db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ASSET);

            // recreate the tables
            onCreate(db);

        }



    public Cursor getAllCustodians(){
    try {
        SQLiteDatabase db_database = getWritableDatabase();

        Cursor cursor = db_database.rawQuery("SELECT * FROM" + TABLE_CUSTODIAN, null);

        if (cursor != null) {
            return cursor;
        } else {
            return null;
        }
    }

    catch(Exception e)
    {
        return null;
    }

    }}

CustodianViewActivity

package com.example.nfcams;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CustodianViewActivity extends Activity {



    ListView CustodianListview;
    DBHandler db_database;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custodian_view);



        db_database = new DBHandler(getApplicationContext());
        CustodianListview = (ListView) findViewById(R.id.custodianlistView);




        new Handler().post(new Runnable() {
            @Override
            public void run() {

                populateCustoListView();

            }
        });


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_custodian_view, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }




    private void populateCustoListView()
   {

       Cursor c = db_database.getAllCustodians();

       CustodianListview.setAdapter(new CustodiansListAdapter(this,c));


   }



    private class CustodiansListAdapter extends CursorAdapter
    {
        private Cursor cursor;
        public CustodiansListAdapter(Context context, Cursor cur) {
            super(context, cur);
            cursor = cur;

        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {


           TextView custID = (TextView) view.findViewById(R.id.Custodianid_view);
           int CustodID = cursor.getInt(cursor.getColumnIndex("_CustID"));
           custID.setText(String.valueOf(CustodID));

            TextView name = (TextView) view.findViewById(R.id.Custodianname_view);
            String Custname = cursor.getString(cursor.getColumnIndex("CustName"));
            name.setText(Custname);

            TextView Designation = (TextView) view.findViewById(R.id.CustodianDesignation_view);
            String CustDesignation = cursor.getString(cursor.getColumnIndex("CustDesign"));
            Designation.setText(CustDesignation);

            TextView Department = (TextView) view.findViewById(R.id.CustodianDepartment_view);
            String CustDepartment = cursor.getString(cursor.getColumnIndex("CustDepart"));
            Department.setText(CustDepartment);

           }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            LayoutInflater inflater = LayoutInflater.from(context);
            View retView = inflater.inflate(R.layout.custodianrow_views, parent, false);
            bindView(retView,context,cursor);
            return retView;
        }




    }



}

Custodianview活动布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.nfcams.CustodianViewActivity">



    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/custodianlistView"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

列出视图布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/list_item_bg"
    android:descendantFocusability="blocksDescendants" >

    <RelativeLayout
        android:id="@+id/layout_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/Custodianname_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dp" />

        <TextView
            android:id="@+id/Custodianid_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/Custodianname_view"
            android:padding="6dp" />

        <TextView
            android:id="@+id/CustodianDesignation_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Custodianid_view"
            android:padding="6dp" />

        <TextView
            android:id="@+id/CustodianDepartment_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/CustodianDesignation_view"
            android:padding="6dp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/layout_item"
        android:background="@color/view_divider_color" />

</RelativeLayout>

错误记录

05-30 09:42:16.471    1334-1334/com.example.nfcams E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.nfcams, PID: 1334
    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.nfcams.CustodianViewActivity$CustodiansListAdapter.<init>(CustodianViewActivity.java:95)
            at com.example.nfcams.CustodianViewActivity.populateCustoListView(CustodianViewActivity.java:84)
            at com.example.nfcams.CustodianViewActivity.access$000(CustodianViewActivity.java:17)
            at com.example.nfcams.CustodianViewActivity$1.run(CustodianViewActivity.java:42)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案