Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中插入数据时,数据库中不存在列_Android_Sqlite_Runtime Error - Fatal编程技术网

在android中插入数据时,数据库中不存在列

在android中插入数据时,数据库中不存在列,android,sqlite,runtime-error,Android,Sqlite,Runtime Error,我试图从用户那里获取输入,然后将其存储在数据库中,但每次我按save。。我在下面复制catlog时出现运行时错误。我到处检查我的代码很多次,但没有得到一个单一的想法是哪里出了问题。请帮帮我。 先发制人 这是我的日志上写的 06-07 14:11:35.168: E/AndroidRuntime(593): FATAL EXCEPTION: main 06-07 14:11:35.168: E/AndroidRuntime(593): java.lang.IllegalStateExcepti

我试图从用户那里获取输入,然后将其存储在数据库中,但每次我按save。。我在下面复制catlog时出现运行时错误。我到处检查我的代码很多次,但没有得到一个单一的想法是哪里出了问题。请帮帮我。 先发制人

这是我的日志上写的

 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
我要存储在数据库中的代码是

 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>
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();
    }

}
public类AddNotes扩展活动{
编辑文本日期;
编辑文字钱;
文本视图类别;
编辑文本描述;
图像按钮保存;
私有NotesDataSource数据源;
@凌驾
创建时的公共void(Bundle icircle){
super.onCreate(icircle);
setContentView(R.layout.addnotes);
数据源=新的NotesDataSource(此);
datasource.open();
日期=(EditText)findViewById(R.id.date);
money=(EditText)findViewById(R.id.money);
category=(TextView)findViewById(R.id.category);
save=(ImageButton)findViewById(R.id.addnote\u save);
description=(EditText)findViewById(R.id.description);
字符串[]类别=新字符串[]{“教育”、“儿童保育”、“膳食”、“旅行”、“家庭”、“其他”};
ListView lv=(ListView)findViewById(R.id.category\U列表);
lv.setAdapter(新的ArrayAdapter(AddNotes.this,android.R.layout.simple_list_item_1,categories));
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//单击后,显示带有文本视图文本的祝酒词
开关(位置)
{
案例0:category.setText(“教育”);
案例1:setText类(“儿童保育”);
案例2:setText类(“膳食”);
案例3:setText类(“旅行”);
案例4:setText类(“系列”);
案例5:setText类(“其他”);
}
}
});
}
公共void onClick(视图){
//@抑制警告(“未选中”)
//ArrayAdapter=(ArrayAdapter)getListAdapter();
Notes=null;
开关(view.getId()){
案例R.id.addnote\u保存:
//将新注释保存到数据库中
note=datasource.createNote(date.getText().toString(),Float.parseFloat(money.getText().toString()),category.toString(),description.toString());
//适配器。添加(注释);
打破
案例R.id.addnote\u取消:
日期:setText(“”);
money.setText(“”);
类别.setText(“”);
description.setText(“”);
Intent newActivity=newintent(这个,receipt.class);
星触觉(新活动);
打破
}
}
@凌驾
受保护的void onResume(){
datasource.open();
super.onResume();
}
@凌驾
受保护的void onPause(){
datasource.close();
super.onPause();
}
}

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


通过执行
money.toString()
您试图将
money
对象转换为字符串,然后转换为
Float
。对每个
editText
对象使用
getText()
方法。它应该可以解决您的问题。

此错误似乎是由
Float.parseFloat(money.toString())
引起的,您的
EditText
不应该包含任何字符串,只能包含数字,请尝试使用
Float.parseFloat(money.getText().toString())


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

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


我的回答解决了你的问题吗?是的。。但现在我面临新的问题,因为我张贴
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;
}
 }
 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);
}

}
 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();
    }

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


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

                break;