时间选择器对话框没有显示,给Fathal Exception?

时间:2014-11-19 11:59:36

标签: android timepicker

我想动态添加字段,点击添加按钮然后使用exittext,settime按钮,删除按钮添加行。现在当点击删除按钮行将被删除其确定,但问题是当点击设置时间按钮然后给出致命异常,对话框不显示。请检查我的代码。我在此代码中的settime按钮有问题。当点击该按钮时不幸关闭。

                      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);
        }
    });        

我的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)

2 个答案:

答案 0 :(得分:1)

尝试使用此

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

而不是

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

答案 1 :(得分:0)

我使用列表视图而不是线性布局,并为动态添加的行充气。与其他人相比,这非常聪明。