【发布时间】:2010-12-02 08:39:10
【问题描述】:
我正在尝试使用对话框包装器将数据插入 SqlLite 数据库。但是,我无法这样做。我有 2 个名为 Title 和 Template 的文本要插入到数据库中。但它崩溃了。
下面是我的java代码:
@Override
公共布尔 onCreateOptionsMenu(菜单菜单)
{
menu.add(0,MENU_ITEM_INSERT,0,R.string.menu_insert);
返回真;
}
@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;
}
}
下面是我的 logCat:
12-02 08:23:17.712:错误/AndroidRuntime(204):未捕获的处理程序:线程主因未捕获的异常而退出
12-02 08:23:17.862: 错误/AndroidRuntime(204): java.lang.NullPointerException
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList$DialogWrapper.getTitle(TemplatesList.java:214)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList.processInsert(TemplatesList.java:196)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList.access$0(TemplatesList.java:192)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 joel.GroupSMS.TemplatesList$1.onClick(TemplatesList.java:150)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 android.os.Handler.dispatchMessage(Handler.java:99)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 android.os.Looper.loop(Looper.java:123)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 android.app.ActivityThread.main(ActivityThread.java:4363)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 java.lang.reflect.Method.invokeNative(Native Method)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 java.lang.reflect.Method.invoke(Method.java:521)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-02 08:23:17.862: 错误/AndroidRuntime(204): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-02 08:23:17.862: 错误/AndroidRuntime(204): at dalvik.system.NativeStart.main(Native Method)
编辑
这是我要求的与我的评论有关的堆栈跟踪:
12-06 16:54:12.466:INFO/ActivityManager(58):开始活动:Intent { cmp=joel.GroupSMS/.TemplateEdit }
12-06 16:54:16.550:调试/AndroidRuntime(254):关闭 VM
12-06 16:54:16.550: WARN/dalvikvm(254): threadid=3: 线程以未捕获的异常退出 (group=0x4001b188)
12-06 16:54:16.550:错误/AndroidRuntime(254):未捕获的处理程序:线程主因未捕获的异常而退出
12-06 16:54:16.736: 错误/AndroidRuntime(254): java.lang.RuntimeException: 无法启动活动 ComponentInfo{joel.GroupSMS/joel.GroupSMS.TemplateEdit}: java.lang.NullPointerException
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.os.Looper.loop(Looper.java:123)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.main(ActivityThread.java:4363)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 java.lang.reflect.Method.invoke(Method.java:521)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-06 16:54:16.736: 错误/AndroidRuntime(254): at dalvik.system.NativeStart.main(Native Method)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 由: java.lang.NullPointerException
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.content.ContentResolver.acquireProvider(ContentResolver.java:754)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.content.ContentResolver.query(ContentResolver.java:197)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.Activity.managedQuery(Activity.java:1495)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 joel.GroupSMS.TemplateEdit.onCreate(TemplateEdit.java:77)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-06 16:54:16.736: 错误/AndroidRuntime(254): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
【问题讨论】: