venkat
venkat

Reputation: 157

time picker dialog is not displaying, give Fathal Exception?

I want to add fields dynamically, when click add button then add row with exittext,settime button,remove button. Now when click remove button row will be removed its ok, but problem is when click set time button then give fatal exception, dialog is not displayed. please check my code. i have problem at settime button in this code. when click that button unfortunately closed.

                      addMore.setOnClickListener(new OnClickListener() {

        @SuppressLint("InflateParams")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LayoutInflater layoutInflater = 
                      (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    final View addView = layoutInflater.inflate(R.layout.row, null);
                    final EditText detext=(EditText)addView.findViewById(R.id.txt_dynamic);

                    // button for settime in row
                    final Button settimebtn=(Button)addView.findViewById(R.id.settime);
                    settimebtn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            //**************************


                                // Process to get Current Time
                                final Calendar c = Calendar.getInstance();
                                mHours = c.get(Calendar.HOUR_OF_DAY);
                                mMinitue = c.get(Calendar.MINUTE);

                                // Launch Time Picker Dialog
                               TimePickerDialog tpdynamic=new TimePickerDialog(getApplicationContext(), new TimePickerDialog.OnTimeSetListener() {

                                @Override
                                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                    // TODO Auto-generated method stub
                                    detext.setText(hourOfDay + ":" + minute);
                                }
                            } , mHours, mMinitue, false);
                                       tpdynamic.show();
                            }
                            //**************************

                    });

                    //button for remove
                    Button remove=(Button)addView.findViewById(R.id.remove);
                    remove.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                             ((LinearLayout)addView.getParent()).removeView(addView);
                        }
                    });
                    container.addView(addView, 0);
        }
    });        

My logcat :

11-19 17:06:06.010: E/AndroidRuntime(15357): FATAL EXCEPTION: main 11-19 17:06:06.010: E/AndroidRuntime(15357): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.view.ViewRootImpl.setView(ViewRootImpl.java:571) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.app.Dialog.show(Dialog.java:281) 11-19 17:06:06.010: E/AndroidRuntime(15357): at com.example.medicine.MedicineHomeActivity$1$1.onClick(MedicineHomeActivity.java:89) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.view.View.performClick(View.java:4217) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.view.View$PerformClick.run(View.java:17368) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.os.Handler.handleCallback(Handler.java:725) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.os.Handler.dispatchMessage(Handler.java:92) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.os.Looper.loop(Looper.java:137) 11-19 17:06:06.010: E/AndroidRuntime(15357): at android.app.ActivityThread.main(ActivityThread.java:5039) 11-19 17:06:06.010: E/AndroidRuntime(15357): at java.lang.reflect.Method.invokeNative(Native Method) 11-19 17:06:06.010: E/AndroidRuntime(15357): at java.lang.reflect.Method.invoke(Method.java:511) 11-19 17:06:06.010: E/AndroidRuntime(15357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 11-19 17:06:06.010: E/AndroidRuntime(15357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-19 17:06:06.010: E/AndroidRuntime(15357): at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 93

Answers (2)

venkat
venkat

Reputation: 157

I used List View instead of Linear Layout, and inflate the Dynamically added row. This very smart compared to others.

Upvotes: 0

Abdul Mohsin
Abdul Mohsin

Reputation: 1435

Try using this

    LayoutInflater layoutInflater = (LayoutInflater)YourActivityName.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

instead of

    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Upvotes: 1

Related Questions