在android中连接数据库时出错

时间:2013-05-02 09:36:05

标签: android sqlite

I am trying to connect the sqlite database with android. I hereby paste the code,

webview.java,


public class webview extends Activity{
EditText ediValue ;
TextView txt;
 DatabaseHandler db;
public void onCreate(Bundle savedInstance)
{

    super.onCreate(savedInstance);
    setContentView(R.layout.activity_main);
    btn.setOnClickListener(new View.OnClickListener() {


    public void onClick(View v) {
        SharedPreferences.Editor editor =     
                                     getPreferences(MODE_PRIVATE).edit();
         editor.putString("text", ediValue.getText().toString());
         editor.putInt("selection-start", ediValue.getSelectionStart());
         editor.putInt("selection-end", ediValue.getSelectionEnd());
         editor.commit();
         String data = getPreferences(MODE_PRIVATE).getString("text","");
         Login login = new Login();
         login.setName(data);
         login.setScore(10);

         db.addScore(login);

    }
});
    }


}

我的Login.java是,

public class Login {

 public String name;
 public int score;
 public String getName(){
     return name;
 }
 public int getScore(){
     return score;
 }
 public void setScore(int s){
     score=s;
 }
 public void setName(String name){
     this.name = name;
 }
}

我的Databasehandler.java是,

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ScoreData ";
private static final String TABLE_SCORE = "score";
private static final String KEY_SCORE = "score";
private static final String KEY_NAME = "name";

 public DatabaseHandler(Context context) {
   super(context, null, null, DATABASE_VERSION);
   }
  public void onCreate(SQLiteDatabase db) {

   String CREATE_SCORE_TABLE="CREATE TABLE " + TABLE_SCORE +"(" + KEY_NAME + " TEXT," + 
             KEY_SCORE+" INTEGER" + ")";
   db.execSQL(CREATE_SCORE_TABLE);
   }
   public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SCORE);
       onCreate(db);
   }


   public void addScore(Login login) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_SCORE, login.getScore());
    values.put(KEY_NAME, login.getName());
    db.insert(TABLE_SCORE, null, values);
    db.close();
   }
   }

现在,当我运行此活动时,我得到了nullpointerexception。任何人都可以告诉这段代码有什么问题。我用的是android 4.2。除了代码之外,我没有对db方面做任何改动。我特此发布例外,

05-02 09:00:44.635: E/Trace(1033): error opening trace file: No such file or directory (2)
05-02 09:00:44.875: E/AndroidRuntime(1033): FATAL EXCEPTION: main
05-02 09:00:44.875: E/AndroidRuntime(1033): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.newandroid/com.example.newandroid.webview}: java.lang.NullPointerException
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.os.Looper.loop(Looper.java:137)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.reflect.Method.invoke(Method.java:511)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at dalvik.system.NativeStart.main(Native Method)
05-02 09:00:44.875: E/AndroidRuntime(1033): Caused by: java.lang.NullPointerException
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at com.example.newandroid.webview.<init>(webview.java:17)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.Class.newInstanceImpl(Native Method)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at java.lang.Class.newInstance(Class.java:1319)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-02 09:00:44.875: E/AndroidRuntime(1033):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-02 09:00:44.875: E/AndroidRuntime(1033):     ... 11 more
05-02 09:03:42.775: E/AndroidRuntime(1414): FATAL EXCEPTION: main

2 个答案:

答案 0 :(得分:0)

您必须首先初始化数据库,如

DatabaseHandler db = new DatabaseHandler(this);

然后

db.addScore(new Login("Your values", "Your Values"));

我想这会奏效。

答案 1 :(得分:0)

在您的网络视图Activity中指定:

 btn.setOnClickListener(new View.OnClickListener() {

但你从未宣布按钮。确保首先使用以下内容声明按钮:

Button btn = (Button) findViewById(R.id.yourbutton);