我想在应用程序保存数据时添加旋转进度对话框。我已经尝试将代码放在AsyncTask
中,但发生错误:
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
此外,AddEditProcessor
还扩展了AsyncTask
,我需要使用其结果来确定不同的操作,因此我必须使用.get()
。但是,日志显示"对话框正在运行"但事实上实际上没有任何东西出现!
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
inflater.inflate(R.menu.note_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.save:
dialog = new ProgressDialog(activity);
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.show();
Log.d(TAG, "dialog is running");
ArrayList<String> errorList = new ArrayList<String>();
boolean edited = false;
for (int i=0; i<answerContainer.length; i++){
boolean errorFlag = false;
LinearLayout ansView = answerContainer[i];
String dqId = (String)questionContainer[i].getTag(R.id.dq_id);
try {
Log.i(TAG, "dq id:"+dqId);
JSONStringer editJson = new JSONStringer().array();
JSONStringer addJson = new JSONStringer().array();
boolean addFlag = false;
boolean editFlag = false;
for(int j=0; j<ansView.getChildCount(); j++){
//Log.i(TAG, "text no.:"+j);
EditText ansText = (EditText)ansView.getChildAt(j);
if (//!ansText.getText().toString().isEmpty() &&
(boolean)ansText.getTag(R.id.edit_flag) == true){
//Log.d(TAG, ansText.getText().toString());
edited = true;
if (!((String)ansText.getTag(R.id.da_id)).isEmpty()){//edit
editJson
.object()
.key(SupremeDB.DANCE_ANS_ID)
.value(ansText.getTag(R.id.da_id))
.key(SupremeDB.DANCE_ANS_CONTENT)
.value(URLEncoder.encode(ansText.getText().toString(), "UTF-8"))
.endObject();
editFlag = true;
} else {//add
addJson
.object()
.key(SupremeDB.DANCE_ANS_CONTENT)
.value(URLEncoder.encode(ansText.getText().toString(), "UTF-8"))
.endObject();
addFlag = true;
}
}
}
editJson.endArray();
addJson.endArray();
if (editFlag){
Log.d(TAG, editJson.toString());
AddEditProcessor editSave = new AddEditProcessor(controller.bniRootUrl+
controller.folderOneToOne+
controller.folderOnetoOneDance+
controller.fileEdit, activity);
editSave.execute(new BasicNameValuePair(SupremeDB.DANCE_ID, danceId),
new BasicNameValuePair(SupremeDB.DANCE_QUE_ID, dqId+""),
new BasicNameValuePair(SupremeDB.DANCE_DATE, datePickerOpener.getText().toString()+" "+timeText.getText().toString()),
new BasicNameValuePair("answers", editJson.toString()));
if (!editSave.get()){
Log.e(TAG, "error occur at edit save");
errorFlag = true;
}
}
if (addFlag){
Log.d(TAG, addJson.toString());
AddEditProcessor addSave = new AddEditProcessor(controller.bniRootUrl+
controller.folderOneToOne+
controller.folderOnetoOneDance+
controller.fileAdd, activity);
addSave.execute(new BasicNameValuePair(SupremeDB.DANCE_ID, danceId),
new BasicNameValuePair(SupremeDB.DANCE_QUE_ID, dqId+""),
new BasicNameValuePair(SupremeDB.DANCE_DATE, datePickerOpener.getText().toString()),
new BasicNameValuePair("answers", addJson.toString()));
if (!addSave.get()){
Log.e(TAG, "error occur at add save");
errorFlag = true;
}
}
if (errorFlag){
errorList.add("q"+(i+1));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (errorList.size()>0){
StringBuilder errMsgBuilder = new StringBuilder();
errMsgBuilder.append("儲存發生錯誤於: ");
for(int n=0; n<errorList.size(); n++){
errMsgBuilder.append(errorList.get(n)).append(",");
}
errMsgBuilder.deleteCharAt(errMsgBuilder.length()-1);
Toast.makeText(activity, errMsgBuilder.toString(), Toast.LENGTH_LONG).show();
} else {
if ((boolean)datePickerOpener.getTag(R.id.edit_flag) ==true || (boolean)timeText.getTag(R.id.edit_flag) ==true || edited == true){
boolean successFlag = true;
if ((boolean)datePickerOpener.getTag(R.id.edit_flag) ==true || (boolean)timeText.getTag(R.id.edit_flag) ==true){
AddEditProcessor saveOneToOne = new AddEditProcessor(controller.bniRootUrl+controller.folderOneToOne+controller.folderOneToOneCalendar+controller.fileEdit, activity);
saveOneToOne.execute(
new BasicNameValuePair(SupremeDB.DANCE_ID, danceId),
new BasicNameValuePair(SupremeDB.USER_ID, user.getUserId()),
new BasicNameValuePair(SupremeDB.DANCE_DATE, datePickerOpener.getText().toString()+" "+timeText.getText().toString()),
new BasicNameValuePair(SupremeDB.DANCE_MEMBER_ID, (String)datePickerOpener.getTag(R.id.dance_member_id))
);
try {
if (saveOneToOne.get()){
LoadingProcessor load = new LoadingProcessor(controller);
LoadProcessor loadProcessor = load.new LoadProcessor(SupremeDB.ONE_TO_ONE_TABLE);
loadProcessor.execute();
JSONObject jsonObject = loadProcessor.get();
if (jsonObject != null){
load.loadIntoDB(SupremeDB.ONE_TO_ONE_TABLE, jsonObject);
} else {
Toast.makeText(activity, getResources().getString(R.string.msg_read_error), Toast.LENGTH_SHORT).show();
successFlag = false;
}
} else {
Toast.makeText(activity, getResources().getString(R.string.msg_sd_error), Toast.LENGTH_SHORT).show();
successFlag = false;
}
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (edited){
LoadingProcessor load = new LoadingProcessor(controller);
LoadProcessor loadProcessor = load.new LoadProcessor(SupremeDB.ONE_TO_ONE_ANSWER_TABLE);
loadProcessor.execute();
try {
JSONObject jsonObject = loadProcessor.get();
if (jsonObject != null){
load.loadIntoDB(SupremeDB.ONE_TO_ONE_ANSWER_TABLE, jsonObject);
} else {
Toast.makeText(activity, getResources().getString(R.string.msg_read_error), Toast.LENGTH_SHORT).show();
successFlag = false;
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (successFlag){
Toast.makeText(activity, getResources().getString(R.string.msg_saved), Toast.LENGTH_SHORT).show();
activity.onBackPressed();
}
}
}
new Thread(new Runnable(){
@Override
public void run()
{
dialog.dismiss();
}
}).start();
break;
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:-1)
Probably you are getting error because of the thread in which you are using the context of the activity.
I would like to refer that You should use the AsyncTask instead of a normal thread. In AsyncTask there is a method onPreExecute() and onPostExecute() which are executing on the main thread and there is a method doInBackground() which will execute on background so that you can easily implement the long live process.