有一个列表视图,我使用AsyncTask从数据库更新。然后,当我单击列表项时,会出现一个带有两个按钮的对话框(一个是取消)。 当我点击另一个按钮时,我在数据库中的另一个表中插入一行。但是当我点击按钮时,应用程序崩溃了。日志文件是这样的.php文件工作正常,列表视图正在更新。
06-24 16:05:54.945: E/AndroidRuntime(15096): FATAL EXCEPTION: AsyncTask #4
06-24 16:05:54.945: E/AndroidRuntime(15096): Process: com.bloodbank.slidingmenu, PID: 15096
06-24 16:05:54.945: E/AndroidRuntime(15096): java.lang.RuntimeException: An error occured while executing doInBackground()
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.AsyncTask$3.done(AsyncTask.java:300)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.lang.Thread.run(Thread.java:818)
06-24 16:05:54.945: E/AndroidRuntime(15096): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.Handler.<init>(Handler.java:200)
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.Handler.<init>(Handler.java:114)
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.widget.Toast$TN.<init>(Toast.java:336)
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.widget.Toast.<init>(Toast.java:100)
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.widget.Toast.makeText(Toast.java:250)
06-24 16:05:54.945: E/AndroidRuntime(15096): at com.bloodbank.slidingmenu.NeedBloodFragment$addBloodRequest.doInBackground(NeedBloodFragment.java:261)
06-24 16:05:54.945: E/AndroidRuntime(15096): at com.bloodbank.slidingmenu.NeedBloodFragment$addBloodRequest.doInBackground(NeedBloodFragment.java:1)
06-24 16:05:54.945: E/AndroidRuntime(15096): at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-24 16:05:54.945: E/AndroidRuntime(15096): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-24 16:05:54.945: E/AndroidRuntime(15096): ... 4 more
代码:
public class NeedBloodFragment extends Fragment {
private ProgressDialog pDialog;
ListView listView1;
JSONParser jParser = new JSONParser();
JSONArray donors = null;
private static final String TAG_SUCCESS = "success";
private static final String TAG_DONORS = "donors";
List<NeedBloodItem> donorslist = new ArrayList<NeedBloodItem>();
public NeedBloodFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_need_blood, container, false);
new LoadAlldonors().execute();
listView1 = (ListView)rootView.findViewById(R.id.listViewNeed);
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
showCustomDialog(donorslist,position,NeedBloodFragment.this.getActivity().getApplicationContext());
}
});
return rootView;
}
class LoadAlldonors extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("need", "A+"));
params.add(new BasicNameValuePair("bgroup", "A+"));
params.add(new BasicNameValuePair("candonate","yes" ));
// getting JSON string from URL
Log.d("ip: ", AppConfig.URL_DONORS);
JSONObject json = jParser.makeHttpRequest(AppConfig.URL_DONORS, "POST", params);
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// donors found
// Getting Array of donors
donors = json.getJSONArray(TAG_DONORS);
// looping through All donors
for (int i = 0; i < donors.length(); i++)
{
JSONObject c = donors.getJSONObject(i);
// Storing each json item in variable
String name = c.getString("name");
String email = c.getString("email");
String contact = c.getString("phone");
// creating new HashMap
donorslist.add(new NeedBloodItem(name,email,contact));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable(){
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapterNeedItem adapter = new ListAdapterNeedItem(getActivity(),
R.layout.need_list_row, donorslist);
listView1.setAdapter(adapter);
}
});
}
}
protected void showCustomDialog(final List<NeedBloodItem> needlist,final int position,Context cnxt) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
TextView tv1 = (TextView)dialog.findViewById(R.id.dialog_tv1);
TextView tv2 = (TextView)dialog.findViewById(R.id.dialog_tv2);
TextView tv3 = (TextView)dialog.findViewById(R.id.dialog_tv3);
tv1.setText(needlist.get(position).name);
tv2.setText(needlist.get(position).phone);
Button button1 = (Button)dialog.findViewById(R.id.dialog_btn1);
Button button2 = (Button)dialog.findViewById(R.id.dialog_btn2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new addBloodRequest().execute();
dialog.dismiss();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
class addBloodRequest extends AsyncTask<String, String, String> {
JSONParser jsonParser = new JSONParser();
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("request", "re"));
params.add(new BasicNameValuePair("donorUID", "12"));
params.add(new BasicNameValuePair("reqUID", "1"));
params.add(new BasicNameValuePair("bloodgroup", "a+"));
params.add(new BasicNameValuePair("reqNAME", "fdee"));
params.add(new BasicNameValuePair("stateName", "dsds"));
params.add(new BasicNameValuePair("city", "svver"));
params.add(new BasicNameValuePair("needDate", "1961-01-01"));
params.add(new BasicNameValuePair("reqPhone", "8888"));
params.add(new BasicNameValuePair("donPhone", "1222"));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(AppConfig.URL_REQUEST,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Toast.makeText(getActivity().getApplicationContext(),
"Request Added to Database", Toast.LENGTH_LONG)
.show();
Log.d("Success","Request Added to Database");
} else {
Toast.makeText(getActivity().getApplicationContext(),
"Request Can't be Added to Database", Toast.LENGTH_LONG)
.show();
Log.e("Error","Request could not be added to Database");
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
}
}
}
编辑:我已经包含了有关问题的代码部分。
答案 0 :(得分:0)
在doInBackground
内使用getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Activity.this, "Message", Toast.LENGTH_SHORT).show();
}
});
来显示吐司
{{1}}
答案 1 :(得分:0)
您尝试在doInbackground中显示Toast消息,这是一个与UI相关的事情。您收到了处理程序错误消息,因为Toast使用了处理程序。
怎么解决? 使用处理程序显示toast消息或调用activity.runOnUIThread ... etc..etc ..