0

我在下面的代码中学习了两个类并将它们合并为一个类。我接近成功,但仍然存在一些错误。我的编程环境告诉我有以下错误:

第 51 行 - 未定义 SQLiteDatabase 类型的方法 open()

第 56 行 - 类型 SQLiteDatabase 的方法 open() 未定义

第 57 行 - SQLiteDatabase 类型的方法 getAllEntries() 未定义

第 98 行 - 无法从 SQLiteOpenHelper 类型对非静态方法 getWritableDatabase() 进行静态引用

第 99 行 - Void 方法不能返回值(我需要在这里返回任何东西吗?)

第 102 行 - 无法从 SQLiteOpenHelper 类型对非静态方法 close() 进行静态引用

我已经尝试重新排列代码以防万一。我不确定当它说“该方法未定义类型 [type]”时是什么意思。使用 Google,我无法确定这些错误消息的含义。

任何帮助将不胜感激。

package com.example.databaseProject;

import android.os.Bundle;
import android.app.Activity;
import android.content.*;
import android.content.DialogInterface.*;
import android.database.*;
import android.database.sqlite.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.content.Context;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class MainActivity extends Activity {
    private SQLiteDatabase db;
    private DBHelper dbHelper;
    private EditText Quote;
    int id=0;
    public static final String KEY_ROWID="_id";
    public static final String KEY_QUOTE="Quote";
    private static final String TAG="DBAdapter";
    private static final String DATABASE_NAME="Random";
    private static final String DATABASE_TABLE="tblRandomQuotes";
    private static final int DATABASE_VERSION=1;
    private static final String DATABASE_CREATE="create table tblRandomQuotes (_id integer primary key autoincrement, "+"Quote text not null );";
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //linkingButton
        Button saveButton=(Button)findViewById(R.id.button1);
        //add onclick listener to saveButton
        saveButton.setonclickListener(mAddListener);
        dbHelper=new DBHelper(this);

    }
    //Create an anonymous implementation of onclickListener
    private onclickListener mAddListener = new onclickListener(){
        public void onclick(View v){
            switch(v.getId()){
                case R.id.button1:
                    db.open();  //line 51
                    long id=0;
                    //do something when the button is clicked
                    try{
                        Quote=(EditText)findViewById(R.id.lastName1);
                        db.insertQuote(Quote.getText().toString());  //line 56
                        id=db.getAllEntries();  //line 57
                        Context context=getApplicationContext();
                        CharSequence text="The quote '"+Quote.getText()+"' was added successfully!\nQuotes Total = "+id;
                        int duration=Toast.LENGTH_LONG;
                        Toast toast=Toast.makeText(context, text, duration);
                        toast.show();
                        Quote.setText("");
                    }
                    catch(Exception ex){
                        Context context=getApplicationContext();
                        CharSequence text=ex.toString()+"ID = "+id;
                        int duration=Toast.LENGTH_LONG;
                        Toast toast=Toast.makeText(context, text, duration);
                        toast.show();
                    }
                    db.close();
                    //break;
               }
           }
    };

    public void DBAdapter(Context ctx){//I think this might go up in the constructor
        this.context=ctx;
        dbHelper=new DBHelper(context);
    }
    private class DBHelper extends SQLiteOpenHelper{
        public DBHelper(Context c){
            super(c,DATABASE_NAME,null,DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db){
            db.execSQL(DATABASE_CREATE);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
            Log.w(TAG,"Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS tblRandomQuotes");
            onCreate(db);
        }
    }
    public void open(){
        db=DBHelper.getWritableDatabase();  //line 98
        return this;  //line 99
    }
    public void close(){
        DBHelper.close();  //line 102
    }
    public long insertQuote(String Quote){
        ContentValues initialValues=new ContentValues();
        initialValues.put(KEY_QUOTE,Quote);
        return db.insert(DATABASE_TABLE,null,initialValues);
    }
    public int getAllEntries(){
        Cursor cursor=db.rawQuery("SELECT COUNT(Quote) FROM tblRandomQuotes", null);
        if(cursor.moveToFirst()){
            return cursor.getInt(0);
        }
        return cursor.getInt(0);
    }

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

}
4

1 回答 1

1

在 onCreate 中删除dbHelper=new DBHelper(this);并添加以下行

DBAdapter(this);

open();  

public void open(){
    db=dbHelper.getWritableDatabase();  //line 98
}

public void close(){
    dbHelper.close();  //line 102
}  

你应该把你的数据库代码变成一个类。

于 2013-04-11T23:09:56.367 回答