Error with inserting data into sqlite database
I'm trying to insert data into SqLite database by using a dialog wrapper. However, I am unable to do so. I have 2 texts called Title and Template to insert into the database. But it crashes. Below is my java code:
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,MENU_ITEM_INSERT,0,R.string.menu_insert); return true; }@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case MENU_ITEM_INSERT:
insert();
//startActivity(new Intent(Intent.ACTION_INSERT,getIntent().getData()));
return true;
}
return(super.onOptionsItemSelected(item));
}
@Override
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo)
{
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
Log.e(TAG, "bad menuInfo", e);
return;
}
mCursor = (Cursor) getListAdapter().getItem(info.position);
menu.setHeaderTitle(mCursor.getString(COLUMN_INDEX_TITLE));
menu.add(0,MENU_ITEM_DELETE,0 ,R.string.menu_delete);
menu.add(1,MENU_ITEM_ADDTMESSAGE,1,R.string.menu_add);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
Log.e(TAG, "bad menuInfo", e);
return false;
}
switch(item.getItemId())
{
case MENU_ITEM_DELETE:
//delete selected row
delete(info.id);
return true;
case MENU_ITEM_ADDTMESSAGE:
//ADD TO MESSAGE
return true;
}
return(super.onContextItemSelected(item));
}
private void insert()
{
LayoutInflater inflater = LayoutInflater.from(this);
View addView = inflater.inflate(R.layout.templates_editor,null);
final DialogWrapper wrapper = new DialogWrapper(addView);
new AlertDialog.Builder(this)
.setTitle(R.string.menu_insert)
.setView(addView)
.setPositiveButton(R.string.item_ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
processInsert(wrapper);
}
})
.setNegativeButton(R.string.item_cancel,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// ignore, just dismiss
}
})
.show();
}
private void delete(final long rowId)
{
if(rowId > 0)
{
new AlertDialog.Builder(this)
.setTitle(R.string.menu_delete)
.setPositiveButton(R.string.item_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
processDelete(rowId);
}
})
.setNegativeButton(R.string.item_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss dialog
}
}).show();
}
}
private void processDelete(long rowId)
{
String[] args = {String.valueOf(rowId)};
db.getWritableDatabase().delete("Templates", Templates._ID, args);
mCursor.requery();
}
private void processInsert(DialogWrapper wrapper)
{
ContentValues cv = new ContentValues();
cv.put("title", wrapper.getTitle());
cv.put("template", wrapper.getTemplate());
db.getWritableDatabase().insert("Templates", "title", cv);
db.getWritableDatabase().insert("Templates", "template", cv);
mCursor.requery();
}
class DialogWrapper{
EditText titleField = null;
EditText templateField = null;
View base = null;
DialogWrapper(View base) {
this.base = base;
}
String getTitle(){
return(getTitleField().getText().toString());
}
String getTemplate(){
return(getTemplateField().getText().toString());
}
private EditText getTitleField(){
if(titleField == null){
titleField = (EditText) findViewById(R.id.title);
}
return titleField;
}
private EditText getTemplateField(){
if(templateField == null){
templateField = (EditText) findViewById(R.id.template);
}
return templateField;
}
}
Below is my logCat:
12-02 08:23:17.712: ERROR/AndroidRuntime(204): Uncaught handler: thread main exiting due to uncaught exception
12-02 08:23:17.862: ERROR/AndroidRuntime(204): java.lang.NullPointerException 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at joel.GroupSMS.TemplatesList$DialogWrapper.getTitle(TemplatesList.java:214) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at joel.GroupSMS.TemplatesList.processInsert(TemplatesList.java:196) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at joel.GroupSMS.TemplatesList.access$0(TemplatesList.java:192) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at joel.GroupSMS.TemplatesList$1.onClick(TemplatesList.java:150) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at android.os.Handler.dispatchMessage(Handler.java:99) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at android.os.Looper.loop(Looper.java:123) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at android.app.ActivityThread.main(ActivityThread.java:4363) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at java.lang.reflect.Method.invokeNative(Native Method) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at java.lang.reflect.Method.invoke(Method.java:521) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-02 08:23:17.862: ERROR/AndroidRuntime(204): at dalvik.system.NativeStart.main(Native Method)Edit
This is my stacktrace requested pertaining to my comment:12-06 16:54:12.466: INFO/ActivityManager(58): Starting activity: Intent { cmp=joel.GroupSMS/.TemplateEdit }
12-06 16:54:16.550: DEBUG/AndroidRuntime(254): Shutting down VM 12-06 16:54:16.550: WARN/dalvikvm(254): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 12-06 16:54:16.550: ERROR/AndroidRuntime(254): Uncaught handler: thread main exiting due to uncaught exception 12-06 16:54:16.736: ERROR/AndroidRuntime(254): java.lang.RuntimeException: Unable to start activity ComponentInfo{joel.GroupSMS/joel.GroupSMS.TemplateEdit}: java.lang.NullPointerException 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.ActivityThread$H.handleM开发者_开发问答essage(ActivityThread.java:1863) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.os.Handler.dispatchMessage(Handler.java:99) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.os.Looper.loop(Looper.java:123) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.ActivityThread.main(ActivityThread.java:4363) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at java.lang.reflect.Method.invokeNative(Native Method) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at java.lang.reflect.Method.invoke(Method.java:521) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at dalvik.system.NativeStart.main(Native Method) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): Caused by: java.lang.NullPointerException 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.content.ContentResolver.acquireProvider(ContentResolver.java:754) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.content.ContentResolver.query(ContentResolver.java:197) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.Activity.managedQuery(Activity.java:1495) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at joel.GroupSMS.TemplateEdit.onCreate(TemplateEdit.java:77) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-06 16:54:16.736: ERROR/AndroidRuntime(254): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)It's hard to tell without line numbers in your code to match the traceback but I'm guessing that (EditText) findViewById(R.id.title)
is returning null. Check your app in hierarchyviewer at the time you're doing the insert and make sure the view you think is there in the hierarchy is actually there at the time you're processing the insert code.
精彩评论