2012-08-01 46 views
3

我有一个使用片段的应用程序,它的工作确定, 但我现在要实现一些弹出窗口当按钮被窃听,的Android PopupWindow

我下面this tutorial "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()) { 



     } 
    } 


} 

那么,我该如何解决这个问题??,在我的片段中显示我的弹出按钮?

回答

14

呼叫

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

ATLEAST它不会显示错误onBaseContext()作为现在需要从相关的活动

编辑

试试这个,

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

+0

嗨,谢谢,我现在有LayoutInflater layoutInflater =(LayoutInflater)getActivity()。getBaseContext()。getSystemService(LAYOUT_INFLATER_SERVICE); ,,但我得到这个错误:LAYOUT_INFLATER_SERVICE无法解析为 变量,请问如何解决这个问题?谢谢! – MaKo 2012-08-01 07:24:38

+0

实际上你是从片段调用它并且它需要来自活动的上下文,所以它不会识别这些服务。它得到的上下文它运作良好。 – Android 2012-08-01 07:28:05

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