从android中的listview适配器启动自定义警报对话框

时间:2014-12-24 05:51:08

标签: android listview

我在做什么::

  • 我有一个listview
  • 我在列表视图的每一行都有一个按钮
  • 我试图在适配器上点击按钮弹出一个对话框 列表视图

发生了什么:

  • 点击按钮后,我收到错误,如日志
  • 所示

在片段中我以这种方式设置适配器

mAdapter = new AdptJobDetail(getActivity().getApplicationContext(),lstData,usrLat,usrLng);      
            lst.setAdapter(mAdapter);

在listview适配器

Context context;
    ArrayList<HashMap<String, String>> lstData;
    private LayoutInflater mInflater;
    String subString;
    String usrLat;
    String usrLng;
    Dialog dialog=null;

    public AdptJobDetail(Context _context,ArrayList<HashMap<String, String>> _lstData, String _usrLat, String _usrLng) {
        context=_context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        lstData=_lstData;
        usrLat=_usrLat;
        usrLng=_usrLng;
    }



<!-----other code ---->


    viewHolder.btnAssignId.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show();

                    ShowConfirmDialog();
                    }
                });


    private void ShowConfirmDialog() {

            dialog = new Dialog(context,android.R.style.Theme_Dialog);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog_confirm_register_vendor);


            Button btnSubmit=(Button)dialog.findViewById(R.id.btn_submit);

            btnSubmit.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });


            dialog.setCanceledOnTouchOutside(false);
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
            dialog.show();
        }

日志:

12-24 11:13:21.984: E/AndroidRuntime(7477): Process: com.windhyaworks, PID: 7477
12-24 11:13:21.984: E/AndroidRuntime(7477): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:554)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.app.Dialog.show(Dialog.java:286)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at com.windhyaworks.adapters.AdptJobDetail.ShowConfirmDialog(AdptJobDetail.java:216)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at com.windhyaworks.adapters.AdptJobDetail.access$0(AdptJobDetail.java:195)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at com.windhyaworks.adapters.AdptJobDetail$2.onClick(AdptJobDetail.java:184)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.view.View.performClick(View.java:4456)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.view.View$PerformClick.run(View.java:18465)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.os.Handler.handleCallback(Handler.java:733)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.os.Looper.loop(Looper.java:136)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at android.app.ActivityThread.main(ActivityThread.java:5086)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at java.lang.reflect.Method.invoke(Method.java:515)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-24 11:13:21.984: E/AndroidRuntime(7477):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

使用getActivity()代替getActivity().getApplicationContext()作为适配器构造函数中的第一个参数:

mAdapter = new AdptJobDetail(getActivity(),lstData,usrLat,usrLng);      
lst.setAdapter(mAdapter);