来自Fragment的Android PopupWindow

3

我有一个使用了碎片的应用程序,目前运行良好, 但是现在我需要在按钮被点击时实现一些弹出窗口。

我正在按照这个教程 "Example of using PopupWindow"进行操作。

但我遇到了以下错误:

Multiple markers at this line
    - LAYOUT_INFLATER_SERVICE cannot be resolved to a variable
    - The method getBaseContext() is undefined for the type new 
     View.OnClickListener(){}

这是我的 .java 文件

public class Tab2HeadH1 extends Fragment   implements OnClickListener{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);

        //Buttons

        Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);

        buttonNose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
              //aqui tus tareas,,

                LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  //ERRORS HERE!!

               View popupView = layoutInflater.inflate(R.layout.popup, null);  
                        final PopupWindow popupWindow = new PopupWindow(
                          popupView, 
                          LayoutParams.WRAP_CONTENT,  
                                LayoutParams.WRAP_CONTENT);

            }


        });


        Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);

        buttonEye.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
               // onLoginClicked(v);
                Toast.makeText(getActivity(), "ss9 eye",
                        Toast.LENGTH_SHORT).show();

            }
        });

return view;
    }



    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);



        ((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
    }

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



        }
    }


}

那么,我该如何解决这个问题?在我的片段中,如何从点击的按钮中显示我的弹出窗口?
2个回答

14

调用

LayoutInflater layoutInflater = (LayoutInflater) **getActivity()**.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);

至少现在使用关联的活动,不会在onBaseContext()上显示错误

编辑

尝试这个:

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


你好,谢谢。我现在有了LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 但是我遇到了这个错误:LAYOUT_INFLATER_SERVICE无法解析为变量,请问如何修复?谢谢! - manuelBetancurt
实际上,您正在从片段调用它,它需要来自活动的上下文,因此它无法识别服务。一旦获得上下文,它就可以正常工作了。 - Android

-1
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);

    View myFragmentView = inflater.inflate(R.layout.yourlayout, container,false);
    //inside button onclick write the code given below
    View popupView = inflater.inflate(R.layout.address, null);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接