0

我正在尝试从用户那里获取输入,然后将其存储在我的数据库中,但每次我按下保存时。我收到运行时错误,我正在下面复制我的目录日志。我四处检查我的代码很多次,但不知道哪里出了问题。请帮我。提前感谢

这就是我的目录中所说的

 06-07 14:11:35.168: E/AndroidRuntime(593): FATAL EXCEPTION: main
 06-07 14:11:35.168: E/AndroidRuntime(593): java.lang.IllegalStateException: Could not execute method of the activity
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.view.View$1.onClick(View.java:2072)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.view.View.performClick(View.java:2408)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.view.View$PerformClick.run(View.java:8816)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.os.Handler.handleCallback(Handler.java:587)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.os.Handler.dispatchMessage(Handler.java:92)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.os.Looper.loop(Looper.java:123)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at java.lang.reflect.Method.invoke(Method.java:521)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at dalvik.system.NativeStart.main(Native Method)
 06-07 14:11:35.168: E/AndroidRuntime(593): Caused by: java.lang.reflect.InvocationTargetException
 06-07 14:11:35.168: E/AndroidRuntime(593):     at com.comp.AddNotes.onClick(AddNotes.java:97)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at java.lang.reflect.Method.invoke(Method.java:521)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.view.View$1.onClick(View.java:2067)
 06-07 14:11:35.168: E/AndroidRuntime(593):     ... 11 more
 06-07 14:11:35.168: E/AndroidRuntime(593): Caused by: android.database.sqlite.SQLiteException: no such column: description: , while compiling: SELECT _id, date, money, category, description FROM notes WHERE _id = -1
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at   android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
  06-07 14:11:35.168: E/AndroidRuntime(593):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
 06-07 14:11:35.168: E/AndroidRuntime(593):     at com.comp.NotesDataSource.createNote(NotesDataSource.java:44)
 06-07 14:11:35.168: E/AndroidRuntime(593):     ... 15 more

我的活动代码

 date = (EditText) findViewById(R.id.date);
        money = (EditText) findViewById(R.id.money);
        category = (TextView) findViewById(R.id.category);
        save= (ImageButton)findViewById(R.id.addnote_save);
        description = (EditText) findViewById(R.id.description);

我要存储在数据库中的代码是

 switch (view.getId()) {
        case R.id.addnote_save:


            note = datasource.createNote(date.toString(),Float.parseFloat(money.toString()),category.toString(),description.toString());

            break;

我的xml是

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

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="80px"
    android:background="@drawable/bluegradbg"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/addnote_cancel"
        android:layout_width="65dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/button_cancel"
        android:onClick="onClick"  />

    <TextView
        android:id="@+id/logo_remember"
        android:layout_width="230px"
        android:layout_height="100px"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="125px"
        android:layout_marginTop="20px"
        android:layout_toRightOf="@+id/menu"
        android:text="Remember it !!"
        android:textColor="#FFFFFF"
        android:textSize="32px"
        android:typeface="sans" />

    <ImageButton
        android:id="@+id/addnote_save"
        android:layout_width="55dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@+id/logo_remember"
        android:src="@drawable/button_save"
        android:onClick="onClick" />
</RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="320px"
        android:background="#FFFFFF"
        android:orientation="vertical" >



        <ImageButton
            android:id="@+id/camera"
            android:layout_width="120px"
            android:layout_height="120px"
            android:layout_marginTop="25px"
             android:layout_marginLeft="15px"
            android:src="@drawable/button_camera" />

        <EditText
            android:id="@+id/date"
            android:layout_width="180px"
            android:layout_height="60px"
            android:layout_marginTop="2px"
             android:layout_marginLeft="25px"
            android:ems="10"
            android:layout_toRightOf="@+id/camera"
            android:inputType="date"
            android:hint="05/06/2012"
             >



        </EditText>

        <TextView
            android:id="@+id/category"
            android:layout_width="180px"
            android:layout_height="60px"
            android:hint="Category"
            android:layout_marginTop="2px"
            android:layout_marginLeft="25px"
            android:layout_centerVertical="true"
            android:ems="10"
            android:layout_toRightOf="@+id/camera"
           >


        </TextView>

        <EditText
            android:id="@+id/money"
           android:layout_width="180px"
            android:layout_height="60px"
            android:layout_marginTop="2px"
            android:layout_marginLeft="25px"
            android:layout_below="@+id/date"
            android:layout_toRightOf="@+id/camera"
            android:layout_centerHorizontal="true"
            android:hint="$0.00"
            android:inputType="numberDecimal" 
            android:ems="10" />

         <EditText
            android:id="@+id/description"
           android:layout_width="440px"
            android:layout_height="350px"
             android:layout_marginTop="2px"
             android:layout_marginLeft="25px"
            android:layout_below="@+id/category"
            android:layout_alignParentLeft="true"
            android:layout_centerHorizontal="true"
            android:inputType="textMultiLine"
            android:background="#FFFFFF"
            android:hint="Type a description of the item or event you'd like us to track for you"
            android:ems="10" />



    </RelativeLayout>

<ListView android:id="@+id/category_list"

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content"></ListView>

</LinearLayout>

NotesDataSource.java 是

package com.comp;

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

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

public class NotesDataSource {

// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
        MySQLiteHelper.COLUMN_DATE ,
         MySQLiteHelper.COLUMN_MONEY,
         MySQLiteHelper.COLUMN_CATEGORY,
         MySQLiteHelper.COLUMN_DESCRIPTION };

public NotesDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

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


public Notes createNote(String date,float money,String category,String description) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_DATE, date);
    values.put(MySQLiteHelper.COLUMN_MONEY, money);
    values.put(MySQLiteHelper.COLUMN_CATEGORY, category);
    values.put(MySQLiteHelper.COLUMN_DESCRIPTION, description);

    long insertId = database.insert(MySQLiteHelper.TABLE_NOTES, null,
            values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_NOTES,
            allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
            null, null, null);
    cursor.moveToFirst();
    Notes newComment = cursorToNotes(cursor);
    cursor.close();
    return newComment;
}

public void deleteNote(Notes note) {
    long id = note.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_NOTES, MySQLiteHelper.COLUMN_ID
            + " = " + id, null);
}



public List<Notes> getAllNotes_Others() {
    List<Notes> comments = new ArrayList<Notes>();



    Cursor cursor = database.rawQuery("SELECT * FROM " + MySQLiteHelper.TABLE_NOTES + " where category = \"Others\"   ", null);

    /*Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns, null, null, null, null, null);*/

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Notes note = cursorToNotes(cursor);
        comments.add(note);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return comments;
}

public List<Notes> getAllNotes() {
    List<Notes> comments = new ArrayList<Notes>();



    Cursor cursor = database.rawQuery("SELECT * from " + MySQLiteHelper.TABLE_NOTES , null);

    /*Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
            allColumns, null, null, null, null, null);*/

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        Notes note = cursorToNotes(cursor);
        comments.add(note);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return comments;
}


private Notes cursorToNotes(Cursor cursor) {
    Notes note = new Notes();
    note.setId(cursor.getLong(0));
    note.setDate(cursor.getString(1));
    note.setMoney(cursor.getFloat(2));
    note.setCategory(cursor.getString(3));
    note.setDescription(cursor.getString(4));
    return note;
}
 }

MySQLiteHelper.java 是

 package com.comp;

 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;

 public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_NOTES = "notes";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_MONEY = "money";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_DESCRIPTION = "description";

private static final String DATABASE_NAME = "notes.db";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_NOTES + "(" + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_DATE
        + " text not null, " + COLUMN_MONEY + " real not null, " + COLUMN_CATEGORY + " text not null, " + COLUMN_DESCRIPTION + "text not null );";

public MySQLiteHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTES);
    onCreate(db);
}

}

addnotes.java 我的活动是

 public class AddNotes extends Activity {

EditText date;
EditText money;
TextView category;
EditText description;
ImageButton save;
private NotesDataSource datasource;



 @Override
    public void onCreate(Bundle icircle) {
        super.onCreate(icircle);
        setContentView(R.layout.addnotes);

        datasource = new NotesDataSource(this);
        datasource.open();


        date = (EditText) findViewById(R.id.date);
        money = (EditText) findViewById(R.id.money);
        category = (TextView) findViewById(R.id.category);
        save= (ImageButton)findViewById(R.id.addnote_save);
        description = (EditText) findViewById(R.id.description);
        String[] categories = new String[] {"Education","Childcare","Meals","Travel","Family","Others"};

        ListView lv = (ListView) findViewById(R.id.category_list);
        lv.setAdapter( new ArrayAdapter<String>(AddNotes.this, android.R.layout.simple_list_item_1, categories)); 


        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
              // When clicked, show a toast with the TextView text
                switch(position)
                {

                case 0 : category.setText("Education");
                case 1 : category.setText("Childcare");
                case 2 : category.setText("Meals");
                case 3 : category.setText("Travel");
                case 4 : category.setText("Family");
                case 5 : category.setText("Others");

                }


            }
          });




 }





 public void onClick(View view) {
        //@SuppressWarnings("unchecked")
        //ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
        Notes note = null;
        switch (view.getId()) {
        case R.id.addnote_save:

            // Save the new comment to the database
            note = datasource.createNote(date.getText().toString(),Float.parseFloat(money.getText().toString()),category.toString(),description.toString());
            //adapter.add(note);
            break;
        case R.id.addnote_cancel:
            date.setText("");
            money.setText("");
            category.setText("");
            description.setText("");
            Intent newActivity = new Intent(this, receipt.class);     
            startActivity(newActivity);
            break;
        }

    }

    @Override
    protected void onResume() {
        datasource.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        datasource.close();
        super.onPause();
    }

}
4

3 回答 3

1

它应该是date.getText().toString()而不是date.toString();

通过这样做money.toString(),您正在尝试将moneyobject 转换为 String ,然后转换为Float. 对每个对象使用getText()方法。editText它应该可以解决您的问题。

于 2012-06-07T07:59:10.750 回答
0

使用此代码而不是您的代码。

为了您的信息,您必须在 EditText 中获取输入的值,这意味着您必须使用getText().

switch (view.getId()) {
            case R.id.addnote_save:


                note = datasource.createNote(date.getText().toString(),Float.parseFloat(money.toString()),category.toString(),description.getText().toString());

                break;
于 2012-06-07T08:24:19.213 回答
0

这个错误似乎是由Float.parseFloat(money.toString()),你EditText不应该包含任何字符串,只有数字,尝试使用Float.parseFloat(money.getText().toString())

EditText不会覆盖toString()以返回其中Editable包含的文本,因此默认情况下Object.toString()会调用。用于getText().toString()从中获取文本EditText

于 2012-06-07T08:00:48.457 回答