我正在處理一個小型項目,其中需要將電話號碼及其相應的地址存儲在Android的sqlite
數據庫中。我的MainActivity
有一個佈局,其中用戶有兩個選項:1.他可以向數據庫添加新值(電話號碼和地址)2.他可以通過輸入電話號碼搜索地址。無法啓動Android中的活動組件信息和Java空指針異常
這是我的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
。所以請原諒任何愚蠢的錯誤,並指出它們。
在此先感謝。
UPDATE 請注意:我已經去爲延伸SQLiteOpenHelper
單獨DataBaseHandler
類解決我的問題現在。我仍然不是爲什麼我得到了我雖然到達這裏的錯誤。現在沒有時間,所以我會在稍後回來,如果有的話我會得到答案。對於那些會遇到這個問題的人,我只想說,製作DataBaseHandler
課程有很大幫助。
在這行你收到此錯誤去了?你的59號線是哪一個? – Nirali 2013-03-28 06:30:32
@Nirali:db。execSQL(CREATE_CONTACTS_TABLE);這是我行號59 – 2013-03-28 06:32:28
的代碼hi @abhi_is_learning_android:請向我們展示您的數據庫幫助類。 – 2013-03-28 06:45:45