0

リストからのテキスト入力を保存するデータベースを作成しています。ただし、修正方法が見つからないというエラーが表示されます。

私のリストコード:

package bookshelf.Android.Java;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import bookshelf.Android.Java.R;


public class Own extends Activity
{
    private EditText item;
    private ListView lv;
    private Toast toast;
    private Button addButton;
    ArrayList<String> items;
    ArrayAdapter<String> listad;
    List<String> books;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.own);
            CharSequence text = "Item added!";
            toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
            setUpView();



    }
    private void setUpView()
    {
        item = (EditText)this.findViewById(R.id.txtAmount);
        lv = (ListView)this.findViewById(R.id.listView1);
        addButton = (Button)this.findViewById(R.id.Add);
        items = new ArrayList<String>();
        items.clear();
        final DataBaseOwn dbo = new DataBaseOwn(Own.this);

        listad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);//need to create a way to get items to equal books
        lv.setAdapter(listad);
        addButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                addItemList();
                books = dbo.get(items);
                toast.show();
            }
        });
        item.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_ENTER)
                {
                    addItemList();
                }
                return true;
            }
        });
    }
    protected void addItemList()
    {
        // TODO Auto-generated method stub

        // TODO Auto-generated method stub
        if (isInputValid(item))
        {
            items.add(0,item.getText().toString());
            item.setText("");
            listad.notifyDataSetChanged();
        }
    }
    protected boolean isInputValid(EditText item2)
    {
        // TODO Auto-generatd method stub
        if (item2.getText().toString().trim().length()<1)
        {
            item2.setError("Please Enter Item");
            return false;
        } 
        else 
        {
            return true;
        }

    }
}

これはデータベースコードです:

package bookshelf.Android.Java;

import java.util.ArrayList;
import java.util.List;

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

public class DataBaseOwn extends SQLiteOpenHelper
{
    public static final int VERSION = 1;
    public static final String TABLE_NAME = "Own List";
    public static final String DBNAME = "ownList.sqlite";
    public static final String ID = "id"; 
    public static final String BOOK = "book";
    static SQLiteDatabase db;

    public DataBaseOwn(Context context)
    {
        super(context, DBNAME, null, VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // TODO Auto-generated method stub
        createDatabase(db);
    }
    private void createDatabase(SQLiteDatabase db)
    {
        db.execSQL("create table " + TABLE_NAME + "(" + ID + "integer primary key autoincrement not null" + BOOK + " text " + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    public Long Insert(String book)
    {
        ContentValues values = new ContentValues();
        values.put(BOOK, book);
        return db.insert(TABLE_NAME, null, values);
    }
    public List<String> get( ArrayList<String> item)
    {
        String[] column = new String[] {BOOK};
        Cursor c = db.query(TABLE_NAME, column, null, null, null, null, null);
        List<String> result = item;
        int columnIndex = c.getColumnIndex(BOOK);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
        {
            result.add(c.getString(columnIndex));
        }
        return result;
    }
}

これらは、コードを投稿された最初の回答に変更する前に発生したエラーです。

   10-17 03:18:42.993: D/dalvikvm(2054): GC_FOR_ALLOC freed 0K, 3% free 10829K/11143K, paused 100ms
    10-17 03:18:43.056: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:18:43.114: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:18:43.523: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:18:43.633: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:18:43.723: D/gralloc_goldfish(2054): Emulator without GPU emulation detected.
    10-17 03:18:44.033: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:18:44.093: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:26.243: D/dalvikvm(2054): GC_FOR_ALLOC freed 542K, 7% free 10369K/11143K, paused 85ms
    10-17 03:19:26.293: I/dalvikvm-heap(2054): Grow heap (frag case) to 12.537MB for 2457616-byte allocation
    10-17 03:19:26.426: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:26.743: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:26.793: D/dalvikvm(2054): GC_CONCURRENT freed 2K, 6% free 12767K/13575K, paused 10ms+8ms
    10-17 03:19:26.903: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:26.963: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:27.413: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:27.483: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:27.675: D/dalvikvm(2054): GC_FOR_ALLOC freed 0K, 6% free 12767K/13575K, paused 89ms
    10-17 03:19:27.773: I/dalvikvm-heap(2054): Grow heap (frag case) to 17.808MB for 5529616-byte allocation
    10-17 03:19:27.903: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:28.073: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:28.133: D/dalvikvm(2054): GC_CONCURRENT freed 0K, 5% free 18167K/19015K, paused 9ms+11ms
    10-17 03:19:28.403: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:28.463: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:28.903: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:29.053: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:29.413: I/dalvikvm(2054): threadid=3: reacting to signal 3
    10-17 03:19:29.504: I/dalvikvm(2054): Wrote stack traces to '/data/anr/traces.txt'
    10-17 03:19:35.773: D/AndroidRuntime(2054): Shutting down VM
    10-17 03:19:35.773: W/dalvikvm(2054): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
    10-17 03:51:39.553: I/dalvikvm(2103): Wrote stack traces to '/data/anr/traces.txt'

    10-17 03:19:35.813: E/AndroidRuntime(2054): FATAL EXCEPTION: main

    10-17 03:19:35.813: E/AndroidRuntime(2054): java.lang.NullPointerException

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at bookshelf.Android.Java.DataBaseOwn.get(DataBaseOwn.java:55)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at bookshelf.Android.Java.Own$1.onClick(Own.java:53)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at android.view.View.performClick(View.java:3511)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at android.view.View$PerformClick.run(View.java:14105)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at android.os.Handler.handleCallback(Handler.java:605)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at android.os.Handler.dispatchMessage(Handler.java:92)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at android.os.Looper.loop(Looper.java:137)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at android.app.ActivityThread.main(ActivityThread.java:4424)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at java.lang.reflect.Method.invokeNative(Native Method)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at java.lang.reflect.Method.invoke(Method.java:511)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

    10-17 03:19:35.813: E/AndroidRuntime(2054):     at dalvik.system.NativeStart.main(Native Method)

These are the new set of errors that I am getting after I put this line in 
db = this.getWritableDatabase();
before line 54.


10-17 04:08:45.765: E/AndroidRuntime(2151): FATAL EXCEPTION: main
10-17 04:08:45.765: E/AndroidRuntime(2151): android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY: , while compiling: create table OwnList(idinteger primary key autoincrement not nullbook text );
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at bookshelf.Android.Java.DataBaseOwn.createDatabase(DataBaseOwn.java:36)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at bookshelf.Android.Java.DataBaseOwn.onCreate(DataBaseOwn.java:32)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at bookshelf.Android.Java.DataBaseOwn.get(DataBaseOwn.java:54)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at bookshelf.Android.Java.Own$1.onClick(Own.java:53)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.view.View.performClick(View.java:3511)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.view.View$PerformClick.run(View.java:14105)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.os.Handler.handleCallback(Handler.java:605)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.os.Looper.loop(Looper.java:137)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at java.lang.reflect.Method.invokeNative(Native Method)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at java.lang.reflect.Method.invoke(Method.java:511)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 04:08:45.765: E/AndroidRuntime(2151):     at dalvik.system.NativeStart.main(Native Method)

うまくフォーマットされていないのが残念です。その方法がわかりません。

4

3 に答える 3

0

"と の間にスペースがありません->メソッドで にinteger変更ID + " integer...しますcreateDatabase(SQLiteDatabase db)

于 2013-11-29T10:16:14.260 に答える
0

あなたのコードにはいくつかの混乱がありますが、次のことが原因で問題が発生する可能性があると思います。

データベースは何も初期化しないため、null です。この null を使用して何かをしようとすると、NullPonterException が発生します。

このすばらしいチュートリアルに従うことをお勧めします。

www.vogella.com/articles/AndroidSQLite/article.html http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/

それがあなたを助けることを願っています。

于 2013-10-26T19:04:36.723 に答える