当前位置: 代码迷 >> Android >> 想照着书上敲个状态栏通知, getSystemService();方法报错 它必须在activity中 但小弟我的按钮在fragment中
  详细解决方案

想照着书上敲个状态栏通知, getSystemService();方法报错 它必须在activity中 但小弟我的按钮在fragment中

热度:86   发布时间:2016-04-28 04:43:30.0
想照着书上敲个状态栏通知, getSystemService();方法报错 它必须在activity中 但我的按钮在fragment中啊
public static class PlaceholderFragment extends Fragment {
private Button button;
private TextView tv;
public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
tv=(TextView)rootView.findViewById(R.id.dd);
   button=(Button)rootView.findViewById(R.id.ss);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
//获取通知管理器
NotificationManager mNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

}

});
像很多这种以前是直接写在activity中  现在控件放fragment中了代码很容易出错啊  求高手指导啊
------解决方案--------------------
getActivity().getSystemService(NOTIFICATION_SERVICE);
------解决方案--------------------
额,fragment可以通过getActivity()获得父activity的context
  相关解决方案