-2

I was trying to call new alert dialog when a new quick action triggers. But am getting error "Can't create handler inside thread that has not called Looper.prepare() ".. Can u please tell me how to solve this issue ??

quickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {          
                public void onItemClick(QuickAction source, int pos, int actionId) {                
                    ActionItem actionItem = quickAction.getActionItem(pos);

                    //here we can filter which action item was clicked with pos or actionId parameter
                    if (actionId == ID_PASSC) {

                    } else if (actionId == ID_PASS) {
                         final Thread t = new Thread() {
                                @Override
                                public void run() {
                                    try {
                                        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        final View layout = inflater.inflate(R.layout.password_dialog, (ViewGroup) findViewById(R.id.root));
                                        final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
                                        final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
                                        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

                                        builder.setTitle("Change password");
                                        builder.setView(layout);
                                        builder.show(); 
                                    } finally {

                                    }
                                }
                            };
                            t.start();
                    } else if (actionId == ID_HELP){

                    }else{

                    }
                }
            });
user1682133
  • 193
  • 2
  • 4
  • 13
  • possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](http://stackoverflow.com/questions/6213538/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – mah Sep 26 '12 at 18:56
  • Please try looking for your answer before posting a question, this has probably been asked 50 times. – Tim Sep 26 '12 at 18:57

1 Answers1

0

you always need to do UI operations on UI thread. I have no idea why you want to create a new thread to create a dialog. Remove creating new thread and show dialog synchronously like below.

} else if (actionId == ID_PASS) {

         LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);                                      final View layout = inflater.inflate(R.layout.password_dialog, (ViewGroup) findViewById(R.id.root));
         final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
         final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
         AlertDialog.Builder builder = new AlertDialog.Builder([ActivityName].this);
         builder.setTitle("Change password");
         builder.setView(layout);
         builder.show(); 
   } 
nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • i tried your code but getting this "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application "..:( sorry am new in android – user1682133 Sep 26 '12 at 19:10
  • ok.. i had just copy pasted your code in middle, I have edited the code, you cannot use getApplicationContext, use ActivityName.this – nandeesh Sep 26 '12 at 19:12
  • oh yes its working now..can u please tell me how can i use uiThread ?? – user1682133 Sep 26 '12 at 19:18
  • right now you are using uithread. Not a rule but usually if you are not doing anything with thread and asynctask you will be executing everything on ui threaad – nandeesh Sep 26 '12 at 19:21