我在 eclipse 中收到以下两个相关错误:
1) The method getIntent() is undefined for the type Fragment1
2) The method setDefaultPushCallback(Context, Class<? extends Activity>) in the type PushService is not applicable for the arguments (FragmentActivity, Class<Fragment1>)
下面是发生错误的代码部分:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container,
false);
ParseAnalytics.trackAppOpened(getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(getActivity(), Fragment1.class);
如果您需要任何说明,请告诉我。
最佳答案
1)您不能在 Fragment 内调用 getIntent()
,因为它不是 Activity,因此不会继承 Activity 的方法/您应该尝试使用 getActivity().getIntent()
如果您确实确定这就是您正在寻找的内容。
2)此方法要求您提供 Context,然后向其传递一个 Activity,然后要求您提供一个扩展 Activity 的类,然后向其传递一个 fragment 。对于第一个参数,我将调用 getActivity().getApplicationContext()
并作为第二个参数 getActivity()
。
编辑:尝试将 getActivity()
转换为:(Activity) getActivity()
ParseAnalytics.trackAppOpened(((Activity)getActivity()).getIntent());
// inform the Parse Cloud that it is ready for notifications
PushService.setDefaultPushCallback(((Activity)getActivity()).getApplicationContext(), ((Activity)getAcitivty()));
关于java - 用 fragment 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26051126/