我正在将记录插入到DB中,该记录在两个字段中具有唯一的构造 - 名称和DOB。我从Async任务的DoInBackground()方法插入。我这样做:
try{
insertInDb(name.trim(),start_date.trim(),imagePath.trim(), String.valueOf(TYPE),String.valueOf(SELECTION), name_friend.trim(), isAdhoc);
}catch (Exception e){
System.out.println("Caught up here"+e);
return "Record Exists";
}
这是我的postExecute():
public void onPostExecute(String result){
//TODO
if(result.equalsIgnoreCase("Record Exists")){
if(Asycdialog !=null && Asycdialog.isShowing())
Asycdialog.dismiss();
Toast.makeText(getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show();
}else if(result.equalsIgnoreCase("Success")){
if(Asycdialog !=null && Asycdialog.isShowing()){
Asycdialog.dismiss();
Intent mainIntent;
mainIntent = new Intent(AdhocCase.this,MainActivity.class);
AdhocCase.this.startActivity(mainIntent);
AdhocCase.this.finish();
}
}
从doInBackground返回的“Record Exists”情况下的toast消息似乎永远不会执行。然而意图确实改变了。
这是我完整的doInBackground():
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
System.out.println("Type in async: "+type);
System.out.println("Selection in async: "+String.valueOf(SELECTION));
System.out.println("TYPE in async: "+String.valueOf(TYPE));
dbHelper.open();
Cursor checkCur = dbHelper.checkIfRecordExists(name.trim(), start_date.trim());
System.out.println("Cursor count"+checkCur.getCount());
if(checkCur != null && checkCur.moveToFirst()){
System.out.println("Exists");
return "Record Exists";
}else{
System.out.println("Image path: "+imagePath);
insertAdhocCase(toTitleCase(name).trim(),start_date.trim(),imagePath, type, String.valueOf(SELECTION), toTitleCase(name_friend).trim());
try{
insertInDb(name.trim(),start_date.trim(),imagePath.trim(), String.valueOf(TYPE),String.valueOf(SELECTION), name_friend.trim(), isAdhoc);
}catch (Exception e){
System.out.println("Caught up here"+e);
return "Record Exists";
}
// LC.writeToSDCard();
}
}catch(Exception e){
// System.out.println(e);
}
return "Success";
}
}
答案 0 :(得分:0)
我认为问题在于您的
Toast.makeText(getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show();
您必须显式提供来自调用Activity的上下文,例如
Toast.makeText(context.getApplicationContext(), "Record Exists please try a different name and year", Toast.LENGTH_LONG).show();
Check this供参考。