0

我正在使用 Eclipse 开发 android 应用程序。应用程序在从数据库中获取数据时强制退出。我在主类中插入并查看数据

我有 2 个类和 1 个布局这是主类

package com.example.anagramslayer;

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

public class Main extends Activity {

 private SQLiteAdapter mySQLiteAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView listContent = (TextView)findViewById(R.id.listview);

        /*
         *  Create/Open a SQLite database
         *  and fill with dummy content
         *  and close it
         */
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();
        //mySQLiteAdapter.deleteAll();
        mySQLiteAdapter.insert("1", "apel",         "Peal",         "the competitor of samsung", "0", "1");
        mySQLiteAdapter.insert("2", "semangka", "Game Sank",    "it's big and green", "0", "1");

        mySQLiteAdapter.close();

        /*
         *  Open the same SQLite database
         *  and read all it's content.
         */
        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToRead();
        String contentRead = mySQLiteAdapter.queueAll();
        mySQLiteAdapter.close();

        listContent.setText(contentRead);

    }
}

这是 SQLiteAdapter 类

package com.example.anagramslayer;

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

public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "id";
 public static final String KEY_QUESTION = "question";
 public static final String KEY_ANSWER = "answer";
 public static final String KEY_HINT = "hint";
 public static final String KEY_FLAG = "flag";
 public static final String KEY_LEVEL = "level";


 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("
 + KEY_ID + " text not null "+ KEY_QUESTION + " text not null " + KEY_ANSWER + " text not null " + KEY_HINT + " text not null" + KEY_FLAG + " text not null " + KEY_LEVEL + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){
  context = c;
 }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }

 public void close(){
  sqLiteHelper.close();
 }

 public long insert(String id, String question, String answer, String hint, String flag, String level){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_ID, id);
  contentValues.put(KEY_QUESTION, question);
  contentValues.put(KEY_ANSWER, answer);
  contentValues.put(KEY_HINT, hint);
  contentValues.put(KEY_FLAG, flag);
  contentValues.put(KEY_LEVEL, level);
  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

 public String queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_QUESTION, KEY_ANSWER, KEY_HINT, KEY_FLAG, KEY_LEVEL};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
    null, null, null, null, null, 
    null);
  String result = "";

  int index_CONTENT = cursor.getColumnIndex(KEY_ID);
  int index_QUESTION = cursor.getColumnIndex(KEY_QUESTION);
  int index_ANSWER = cursor.getColumnIndex(KEY_ANSWER);
  int index_HINT = cursor.getColumnIndex(KEY_HINT);
  int index_FLAG = cursor.getColumnIndex(KEY_FLAG);
  int index_LEVEL = cursor.getColumnIndex(KEY_LEVEL);
  for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
   result = result + cursor.getString(index_CONTENT) + " " + cursor.getString(index_QUESTION)
           + " " + cursor.getString(index_ANSWER) + " " + cursor.getString(index_HINT)
           + " " + cursor.getString(index_FLAG )+ " " + cursor.getString(index_LEVEL) + "\n";
  }

  return result;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

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

  }

 }

}

最后这是 main.xml

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

<TextView
 android:id="@+id/listview"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"/>
</LinearLayout>

有谁知道它有什么问题?

4

0 回答 0