Reputation: 163
i am developing an application which is gets some data from a web service and display in a list view. i use custom adapter for the list view which is extends by Base Adapter class. and i have use Assync Task for some actions such as for calling the web service and get the responce from it and shows in a Toast (in my list view there is two buttons[Accept and Reject] in each row. when someone clicks Accept it calls a web service ).
so i have implemented my assync tasks inside the Adapter class.
my problem is : sometimes my app crashes when i clicks the list butons.[when running those assync task]. but sometimes it works properly.
the error is something like :
01-08 12:20:26.919: E/AndroidRuntime(21754): FATAL EXCEPTION: AsyncTask #3
01-08 12:20:26.919: E/AndroidRuntime(21754): java.lang.RuntimeException: An error occured while executing doInBackground()
01-08 12:20:26.919: E/AndroidRuntime(21754): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-08 12:20:26.919: E/AndroidRuntime(21754): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-08 12:20:26.919: E/AndroidRuntime(21754): at java.lang.Thread.run(Thread.java:856)
01-08 12:20:26.919: E/AndroidRuntime(21754): Caused by: java.lang.RuntimeException: Only one Looper may be created per thread
01-08 12:20:26.919: E/AndroidRuntime(21754): at android.os.Looper.prepare(Looper.java:78)
01-08 12:20:26.919: E/AndroidRuntime(21754): at android.os.Looper.prepare(Looper.java:73)
01-08 12:20:26.919: E/AndroidRuntime(21754): at com.jsonlist.jsonlist.NewsRowAdapter$ShowResponceForReject.doInBackground(NewsRowAdapter.java:646)
01-08 12:20:26.919: E/AndroidRuntime(21754): at com.jsonlist.jsonlist.NewsRowAdapter$ShowResponceForReject.doInBackground(NewsRowAdapter.java:1)
can anyone tell me where the bug is ?
these are the codes it refers to
public void dialogshow(final String appid){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes Confirm!!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// new ShowResponceForAccept().execute("");
//dialog.dismiss();
Handler mHandler=new Handler();
mHandler.post(new Runnable()
{
public void run()
{
new ShowResponceForAccept().execute(appid);
}
});
/* ShortList sendandget = new ShortList();
//long appoinmentID = Long.valueOf(appid);
//long user = Long.valueOf(Uid);
String resp = sendandget.getResponceFromServer(appid,"",Uid);
Appid = null;
Uid = null;
//Toast.makeText(mContext, resp, Toast.LENGTH_LONG).show();
loadListAgain();*/
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
alertDialogBuilder.show();
}
this is the assync task which is inside the same adapter class
public class ShowResponceForAccept extends AsyncTask<String, Void, String>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);
}
@SuppressWarnings("static-access")
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
Looper.myLooper().prepare();
ShortList sendandget = new ShortList();
String app = arg0[0];
//long appoinmentID = Long.valueOf(app);
//long user = Long.valueOf(Uid);
String resp = sendandget.getResponceFromServer(app,"",Uid);
loadListAgain();
//Thread.sleep(10000);
//String x="Your Request Accepted";
return resp;
//return x;
}
@Override
protected void onPostExecute(String resp) {
// TODO Auto-generated method stub
if(pDialog != null){
pDialog.dismiss();
}
if(resp.equalsIgnoreCase("true")){
Toast.makeText(mContext, "Your Request has been Successfully Accepted!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(mContext, "Sorry! Try again Later!", Toast.LENGTH_LONG).show();
}
}
}
can some one help me for this ?
Upvotes: 0
Views: 97
Reputation: 2866
The problem is with
public class ShowResponceForAccept extends AsyncTask<String, Void, String>{
Inside this method your calling
Looper.myLooper().prepare();
You should remove UI updation code from doInBackground and push it in postExecute and remove the Looper. That should solve the problem
Upvotes: 1
Reputation: 14315
Issue is there,
You are try to open Dialog In between AsyankTask execute. Do't update UI when AsysnkTask is Running,Update onPostExecute.
Upvotes: 0