创建数据库SQLqlite

时间:2014-08-03 10:09:31

标签: java android eclipse android-intent classcastexception

我见过很多教程。我做到了这一点:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;

public class AssetDatabaseOpenHelper {

    private static final String DB_NAME = "danitest.db";

    private Context context;

    public AssetDatabaseOpenHelper(OnClickListener onClickListener) {
        this.context = (Context) onClickListener;
    }

    public SQLiteDatabase openDatabase() {
        File dbFile = context.getDatabasePath(DB_NAME);

        if (!dbFile.exists()) {
            try {
                copyDatabase(dbFile);
            } catch (IOException e) {
                throw new RuntimeException("Error creating source database", e);
            }
        }

        return SQLiteDatabase.openDatabase(dbFile.getPath(), null,        SQLiteDatabase.OPEN_READONLY);
    }

    private void copyDatabase(File dbFile) throws IOException {
        InputStream is = context.getAssets().open(DB_NAME);
        OutputStream os = new FileOutputStream(dbFile);

        byte[] buffer = new byte[1024];
        while (is.read(buffer) > 0) {
            os.write(buffer);
        }

        os.flush();
        os.close();
        is.close();
    }

}

数据库插入此目录../ assets / dani.db

启动pubblic:

public class javapage1 extends Activity {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newinterface);
        Button btnHome=(Button)findViewById(R.id.button1);
        btnHome.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(this);
                SQLiteDatabase db = adb.openDatabase();
                Cursor c = db.rawQuery("SELECT * FROM xxx;", null);
                Log.d("MyApp", "cnt: "+c.getCount());
            }
        });
    }
    }

Eclipse报告错误... Log Cat ..:

08-03 08:27:11.759: D/AndroidRuntime(816): Shutting down VM
08-03 08:27:11.759: W/dalvikvm(816): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-03 08:27:11.809: E/AndroidRuntime(816): FATAL EXCEPTION: main
08-03 08:27:11.809: E/AndroidRuntime(816): java.lang.ClassCastException: package.javapage1$1
08-03 08:27:11.809: E/AndroidRuntime(816):  at package.AssetDatabaseOpenHelper.<init>(AssetDatabaseOpenHelper.java:20)
08-03 08:27:11.809: E/AndroidRuntime(816):  at package.javapage1$1.onClick(javapage1.java:22)
08-03 08:27:11.809: E/AndroidRuntime(816):  at android.view.View.performClick(View.java:2408)
08-03 08:27:11.809: E/AndroidRuntime(816):  at android.view.View$PerformClick.run(View.java:8816)
08-03 08:27:11.809: E/AndroidRuntime(816):  at android.os.Handler.handleCallback(Handler.java:587)
08-03 08:27:11.809: E/AndroidRuntime(816):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-03 08:27:11.809: E/AndroidRuntime(816):  at android.os.Looper.loop(Looper.java:123)
08-03 08:27:11.809: E/AndroidRuntime(816):  at android.app.ActivityThread.main(ActivityThread.java:4627)
08-03 08:27:11.809: E/AndroidRuntime(816):  at java.lang.reflect.Method.invokeNative(Native Method)
08-03 08:27:11.809: E/AndroidRuntime(816):  at java.lang.reflect.Method.invoke(Method.java:521)
08-03 08:27:11.809: E/AndroidRuntime(816):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-03 08:27:11.809: E/AndroidRuntime(816):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-03 08:27:11.809: E/AndroidRuntime(816):  at dalvik.system.NativeStart.main(Native Method)
08-03 08:27:13.659: I/Process(816): Sending signal. PID: 816 SIG: 9

1 个答案:

答案 0 :(得分:3)

java.lang.ClassCastException: package.javapage1$1
  at package.AssetDatabaseOpenHelper.<init>(AssetDatabaseOpenHelper.java:20)
ClassCastException中的

<init>,即构造函数或成员变量初始化:

public AssetDatabaseOpenHelper(OnClickListener onClickListener) {
    this.context = (Context) onClickListener;
}

您无法将OnClickListener投射到Context。将其更改为

public AssetDatabaseOpenHelper(Context context) {
    this.context = context;
}

并在OnClickListener中使用活动this而不是OnClickListener匿名内部班this

AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(javapage1.this);