0

SQLiteExample.java

package com.example.myfirstapp;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SQLiteExample extends Activity implements OnClickListener {

 EditText sqlName, sqlHotness;
  Button sqlUpdate, sqlView;

    @Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqliteexample);

    sqlName = (EditText) findViewById(R.id.etName);
    sqlHotness = (EditText) findViewById(R.id.etSQLHotness);
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
    sqlView = (Button) findViewById(R.id.bSQLOpenView);

    sqlUpdate.setOnClickListener(this);
    sqlView.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.bSQLUpdate:

        boolean didItWork = true;
        try {
            String name;
            name = sqlName.getText().toString();
            String hot;
            hot = sqlHotness.getText().toString();
            HotOrNot entry = new HotOrNot(SQLiteExample.this);
            entry.open();
            entry.createEntry(name, hot);
            entry.close();
        } catch (Exception e) {
            didItWork = false;
            String error;
            error = e.toString();
            Dialog d = new Dialog(this);
            TextView tv = new TextView(this);
            d.setTitle("Nop");
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        } finally {

            if (didItWork) {
                Dialog d = new Dialog(this);
                TextView tv = new TextView(this);
                d.setTitle("YAY");
                tv.setText("Success");

                d.setContentView(tv);
                d.show();
            }
        }


        /*
         * 
         * boolean didwork = true; try{ String name =
         * sqlName.getText().toString(); String hotness =
         * sqlHotness.getText().toString();
         * 
         * HotOrNot entry = new HotOrNot(SQLiteExample.this); entry.open();
         * entry.createEntry(name,hotness); entry.close(); }catch (Exception
         * e){ didwork = false; }finally{ if (didwork){ /*Dialog d = new
         * Dialog(this); d.setTitle("Heck Ya!!"); TextView tv = new
         * TextView(this); tv.setText("Success"); d.setContentView(tv);
         * d.show();
         */
        /*
         * Toast t = Toast.makeText(SQLiteExample.this, "Success",
         * Toast.LENGTH_LONG); t.show(); } else{ Toast t =
         * Toast.makeText(SQLiteExample.this, "Fail", Toast.LENGTH_LONG);
         * t.show(); } }
         */
        break;
    case R.id.bSQLOpenView:
        break;
    }
}
  }

HotOrNot.java

package com.example.myfirstapp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HotOrNot extends Activity{

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";

private static final String DATABASE_NAME = "HotOrNotdb.db";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
                + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public HotOrNot(Context c) {

    ourContext = c;
}

public HotOrNot open() throws SQLException {

    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;

}

public void close() {
    ourHelper.close();
}

public long createEntry(String name, String hotness) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}
}

sqlite示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView21"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name" />

<EditText
    android:id="@+id/etSQLName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >

</EditText>

<TextView
    android:id="@+id/textView12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hotness scale 1-10" />

<EditText
    android:id="@+id/etSQLHotness"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

<Button
    android:id="@+id/bSQLUpdate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Update SQLite Database" />

<Button
    android:id="@+id/bSQLOpenView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="View" />

 </LinearLayout>

我有这两个类文件和xml,没有任何错误。但是,当尝试更新 SQLite 数据库时,我设置的对话框会引发错误:java.lang.NullPointerException。我对此很陌生,并且一直在关注 youtube 上的教程。(http://www.youtube.com/watch?v=9ew_Ajpqwqg&list=EC2F07DBCDCC01493A

任何帮助是极大的赞赏。

4

1 回答 1

0

这个

sqlName = (EditText) findViewById(R.id.etName); 

应该

 sqlName = (EditText) findViewById(R.id.etSQLName); 

在您的 xml 中,您的 edittext id 为 etSQLName

 <EditText
 android:id="@+id/etSQLName"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:ems="10" >

除此之外,我在我的设备上尝试了您的代码,它可以工作。我没有收到任何错误。我确实在对话框中收到一条成功消息。

于 2013-06-06T05:05:27.430 回答