-1

我对android中的数据库完全陌生,并尝试在android中使用sqlite制作一个简单的应用程序。但是我的代码有一些问题,或者我找不到问题所在,当我运行我的项目时,应用程序意外停止,而且数据库未创建..请帮助我..我的代码如下:

MainActivity.java

包 com.example.dbdemo1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {
Button ins,del,upd;
ListView lv;
DbHelper dh;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ins = (Button)findViewById(R.id.button1);
        upd = (Button)findViewById(R.id.button2);
        del = (Button)findViewById(R.id.button3);

        lv = (ListView)findViewById(R.id.listView1);
        setUpList();
        dh = new DbHelper(this);
        Cursor c =getAllData();
        registerForContextMenu(lv);
       ins.setOnClickListener((OnClickListener) this);
       upd.setOnClickListener((OnClickListener) this);
       }
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v.equals(ins))
        {
            startActivity(new Intent(MainActivity.this, insertActivity.class));
        }
        else
        {
            //startActivity(new Intent(MainActivity.this, updateActivity.class));
        }
    }
    private Cursor getAllData() {
        // TODO Auto-generated method stub
        return null;
    }

    private void setUpList() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

DBhelper.java

package com.example.dbdemo1;

import android.R.string;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper{


    public static String db_name ="myDB.db";
    public static String fname = "first name";
    public static String lname = "last name";
    public static String contact = "contact";

    public DbHelper(Context context) {
        super(context, db_name, null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try 

        {
            db.execSQL("CREATE TABLE register(_ID INTEGER PRIMARY KEY AUTOINCREMENT,fName TEXT,lNmae TEXT,Contact TEXT);");
        } 
        catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        android.util.Log.v("Constants", "Upgrading database which will destroy all data");
        onCreate(db);

    }

}

日志猫

01-22 07:54:40.003: W/dalvikvm(32581): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-22 07:54:40.013: E/AndroidRuntime(32581): FATAL EXCEPTION: main
01-22 07:54:40.013: E/AndroidRuntime(32581): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbdemo1/com.example.dbdemo1.MainActivity}: java.lang.ClassCastException: com.example.dbdemo1.MainActivity
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.os.Looper.loop(Looper.java:123)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.ActivityThread.main(ActivityThread.java:4627)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at java.lang.reflect.Method.invokeNative(Native Method)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at java.lang.reflect.Method.invoke(Method.java:521)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at dalvik.system.NativeStart.main(Native Method)
01-22 07:54:40.013: E/AndroidRuntime(32581): Caused by: java.lang.ClassCastException: com.example.dbdemo1.MainActivity
01-22 07:54:40.013: E/AndroidRuntime(32581):    at com.example.dbdemo1.MainActivity.onCreate(MainActivity.java:33)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-22 07:54:40.013: E/AndroidRuntime(32581):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-22 07:54:40.013: E/AndroidRuntime(32581):    ... 11 more

请帮助我...提前谢谢你。!

4

2 回答 2

0

要创建数据库,您需要调用

dh.getWritableDatabase();
于 2013-01-22T08:39:10.093 回答
0

您需要实现 OnClickListener 接口才能在 Button 上使用 setOnClickListener。

public class MainActivity extends Activity implements OnClickListener{

或者

您可以为特定按钮创建匿名方法,例如

ins.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
      }
 });

没有实现 OnClickListner 接口。

于 2013-01-22T11:32:15.750 回答