我正在做一个小项目,我需要在Android中将电话号码及其各自的地址存储到sqlite
数据库中。我的MainActivity
具有一个布局,其中用户有两个选项:1.他可以向数据库添加一个新值(电话号码和地址)2.他可以通过输入电话号码来搜索地址。
这是我的MainActivity:
public class MainActivity extends Activity {
private static final String TABLE_CONTACTS = "contacts";
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_ADDRESS = "name";
private static final String KEY_PH_NO = "phone_number";
SQLiteDatabase db;
TextView numberTextview,addressTextView,fetchedAddressTextView;
Button buttonSave,buttonSearch;
EditText numberEditText,addressEditText,searchEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberTextview=(TextView)findViewById(R.id.textView1);
numberEditText=(EditText)findViewById(R.id.editText1);
addressTextView=(TextView)findViewById(R.id.textView2);
addressEditText=(EditText)findViewById(R.id.editText2);
buttonSave=(Button)findViewById(R.id.button1);
searchEditText=(EditText)findViewById(R.id.editText3);
buttonSearch=(Button)findViewById(R.id.button2);
fetchedAddressTextView=(TextView)findViewById(R.id.textView3);
try {
String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_ADDRESS + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
Toast.makeText(MainActivity.this, "table created ", Toast.LENGTH_LONG).show();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
buttonSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String address=addressEditText.getText().toString();
long number = Integer.parseInt(numberEditText.getText().toString());
try {
ContentValues values = new ContentValues();
values.put(KEY_ADDRESS, address); // Contact Address
values.put(KEY_PH_NO, number); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
buttonSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
long phone_number=Integer.parseInt(searchEditText.getText().toString());
try {
Cursor c=db.query(TABLE_CONTACTS, new String[]{
KEY_ADDRESS
},
KEY_PH_NO + "=" + phone_number,
null,
null,
null,
null);
if (c != null) {
c.moveToFirst();
}
fetchedAddressTextView.setText(c.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Logcat跟踪:
03-28 11:45:12.562: E/AndroidRuntime(4079): FATAL EXCEPTION: main
03-28 11:45:12.562: E/AndroidRuntime(4079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fivetofive/com.example.fivetofive.MainActivity}: java.lang.NullPointerException
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.os.Looper.loop(Looper.java:137)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.ActivityThread.main(ActivityThread.java:4895)
03-28 11:45:12.562: E/AndroidRuntime(4079): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 11:45:12.562: E/AndroidRuntime(4079): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 11:45:12.562: E/AndroidRuntime(4079): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-28 11:45:12.562: E/AndroidRuntime(4079): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-28 11:45:12.562: E/AndroidRuntime(4079): at dalvik.system.NativeStart.main(Native Method)
03-28 11:45:12.562: E/AndroidRuntime(4079): Caused by: java.lang.NullPointerException
03-28 11:45:12.562: E/AndroidRuntime(4079): at com.example.fivetofive.MainActivity.onCreate(MainActivity.java:59)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.Activity.performCreate(Activity.java:5163)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-28 11:45:12.562: E/AndroidRuntime(4079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-28 11:45:12.562: E/AndroidRuntime(4079): ... 11 more
该应用程序甚至无法启动。运行时崩溃。这是我第一次在android中使用
sqlite
。因此,请原谅任何愚蠢的错误,并指出它们。提前致谢。
更新
请注意:我已经通过扩展
DataBaseHandler
的单独的SQLiteOpenHelper
类解决了我的问题。我仍然不明白为什么我得到了错误,但是我却到达了这里。现在没有时间,因此,如果我完全可以得到答案,请稍后再回来。对于那些遇到这个问题的人,我只想说制作DataBaseHandler
类有很大帮助。 最佳答案
而且您必须extends SQLiteOpenHelper {
请使用下面的行初始化您的数据库对象
db = new SQLiteDatabase();
并在清单中添加了这一行
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
请仔细阅读本教程
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
http://www.vogella.com/articles/AndroidSQLite/article.html