本文介绍了创建数据库SQLqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了很多的教程。我已经做到了这一点:

 进口的java.io.File;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口android.content.Context;
进口android.database.sqlite.SQLiteDatabase;
进口android.view.View.OnClickListener;

公共类AssetDatabaseOpenHelper {

    私有静态最后弦乐DB_NAME =danitest.db;

    私人上下文的背景下;

    公共AssetDatabaseOpenHelper(OnClickListener onClickListener){
        this.context =(上下文)onClickListener;
    }

    公共SQLiteDatabase的openDatabase(){
        文件DBFILE = context.getDatabasePath(DB_NAME);

        如果(!dbFile.exists()){
            尝试 {
                copyDatabase(DBFILE);
            }赶上(IOException异常E){
                抛出新的RuntimeException(错误创建源数据库,E);
            }
        }

        返回SQLiteDatabase.openDatabase(dbFile.getPath(),空,SQLiteDatabase.OPEN_READONLY);
    }

    私人无效copyDatabase(文件DBFILE)抛出IOException异常{
        InputStream的是= context.getAssets()开(DB_NAME)。
        的OutputStream OS =新的FileOutputStream(DBFILE);

        byte []的缓冲区=新的字节[1024];
        而(is.read(缓冲液)大于0){
            os.write(缓冲液);
        }

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

}
 

在此目录中数据库插入../资产/ dani.db

开始pubblic:

 公共类javapage1延伸活动{

@覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.newinterface);
        按钮btnHome =(按钮)findViewById(R.id.button1);
        btnHome.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                AssetDatabaseOpenHelper ADB =新AssetDatabaseOpenHelper(本);
                SQLiteDatabase DB = adb.openDatabase();
                光标C = db.rawQuery(SELECT * FROM XXX;,NULL);
                Log.d(MyApp的,CNT:+ c.getCount());
            }
        });
    }
    }
 

Eclipse中报错...登录猫..:

 八月八日至3日:27:11.759:D / AndroidRuntime(816):关闭虚拟机
八月8号至3号:27:11.759:W / dalvikvm(816):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
八月8号至3号:27:11.809:E / AndroidRuntime(816):致命异常:主要
八月8号至3号:27:11.809:E / AndroidRuntime(816):java.lang.ClassCastException:package.javapage1 $ 1
八月8号至3号:27:11.809:E / AndroidRuntime(816):在package.AssetDatabaseOpenHelper< INIT>(AssetDatabaseOpenHelper.java:20)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在package.javapage1 $ 1.onClick(javapage1.java:22)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在android.view.View.performClick(View.java:2408)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在android.view.View $ PerformClick.run(View.java:8816)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在android.os.Handler.handleCallback(Handler.java:587)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在android.os.Handler.dispatchMessage(Handler.java:92)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在android.os.Looper.loop(Looper.java:123)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在android.app.ActivityThread.main(ActivityThread.java:4627)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在java.lang.reflect.Method.invokeNative(本机方法)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在java.lang.reflect.Method.invoke(Method.java:521)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
八月8号至3号:27:11.809:E / AndroidRuntime(816):在dalvik.system.NativeStart.main(本机方法)
八月8号至3号:27:13.659:I /处理(816):发送信号。 PID:816 SIG:9
 

解决方案

  java.lang.ClassCastException:package.javapage1 $ 1
  在package.AssetDatabaseOpenHelper< INIT>(AssetDatabaseOpenHelper.java:20)
 

ClassCastException异常< INIT> 即构造函数或成员变量的初始化:

 公共AssetDatabaseOpenHelper(OnClickListener onClickListener){
    this.context =(上下文)onClickListener;
}
 

您不能施放一个 OnClickListener 上下文。更改为类似

 公共AssetDatabaseOpenHelper(上下文的背景下){
    this.context =背景;
}
 

OnClickListener 使用活动这样不是 OnClickListener 匿名内部类

  AssetDatabaseOpenHelper ADB =新AssetDatabaseOpenHelper(javapage1.this);
 

I've seen a lot of tutorials. I've done this:

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

}

Database insert in this directory ../assets/dani.db

Start 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 report error... 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
解决方案
java.lang.ClassCastException: package.javapage1$1
  at package.AssetDatabaseOpenHelper.<init>(AssetDatabaseOpenHelper.java:20)

ClassCastException in <init> i.e. constructor or member variable initialization:

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

You cannot cast an OnClickListener to a Context. Change this to something like

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

and in the OnClickListener use the activity this instead of the OnClickListener anonymous inner class this:

AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(javapage1.this);

这篇关于创建数据库SQLqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 11:23