-3

There is a list view which i'm updating from database using AsyncTask. Then when i click on the list item a dialog box appears with two buttons(one is cancel). When i click on the other button , i am inserting a row in another table in the database. But when i click on the button, the app crashes. The log file is like this.the php files are working fine and the list view is updating nicely.

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

The code:

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

        }

    }

}

edit: i've included the parts of the code concerning the problem.

divya
  • 3
  • 3
  • I think you are making a `Toast.maketText()` in the `doInBackGround()` Method. You cant do UI operations in a background Thread. Post your code. – Emil Jun 24 '15 at 10:49
  • take a look at this: http://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare – Rich Jun 24 '15 at 10:50
  • OffTopic: onPostExecute() is running on the UI thread so you don't need to call runOnUiThread() – drewi Jun 24 '15 at 10:59
  • @Boss Toast.makeText() is OK for background, the problem here is in show() method it should be executed on UI thread – Viktor Yakunin Jun 24 '15 at 11:08
  • @ViktorYakunin sorry..Thats what i meant though.. – Emil Jun 24 '15 at 11:38
  • why have so many people downvoted the question? the question is quite clear as well as useful and i've also included the code. – divya Jun 24 '15 at 17:03

2 Answers2

0

use runOnUiThreadinside doInBackground to show toast

getActivity().runOnUiThread(new Runnable() {                
    @Override
    public void run() {                         
        Toast.makeText(Activity.this, "Message", Toast.LENGTH_SHORT).show();
    }
});
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
  • just an edit: it's a fragment. so it should be getActivity().runOnUiThread(new Runnable()) – divya Jun 24 '15 at 11:03
  • Yeah Cool Edited My Post too. Happy u got working. Mark it accepted so other users might find easy to go through the solution easily. – Neal Ahluvalia Jun 24 '15 at 11:04
0

you are try to show a Toast message in doInbackground ,which is a UI related thing. You got the handler error message, because Toast uses handler.

how to solve ? Use a handler to show toast message or call activity.runOnUIThread ...etc..etc..

Please see the original source code of toast: http://grepcode.com/file_/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-0/android/widget/Toast.java/?v=source

narancs
  • 5,234
  • 4
  • 41
  • 60