1
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。谁能说出这段代码有什么问题。我使用安卓 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
4

2 回答 2

0

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

DatabaseHandler db = new DatabaseHandler(this);

接着

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

我想这会奏效。

于 2013-05-02T09:50:55.000 回答
0

在您的网络视图中Activity,您指定以下内容:

 btn.setOnClickListener(new View.OnClickListener() {

但是您从未声明过按钮。确保首先使用以下内容声明按钮:

Button btn = (Button) findViewById(R.id.yourbutton);
于 2013-05-02T09:58:05.260 回答