findViewById未被识别为SherlockFragment中的方法

时间:2013-10-28 12:16:08

标签: android android-fragments android-fragmentactivity

我有一个里面有两个按钮的片段。我无法将xml按钮附加到java按钮,因为在SherlockFragment类中,findViewById不会被识别为方法。

我将SherlockFragment更改为SherlockFragmentActivity,但是这样,OnCreateView方法不适合使用,我不知道应该使用哪种方法?

变式1:findViewById无法识别为方法

public class MyProfileActionButtonsFragment extends SherlockFragment {
    private Button bMyProfileEditProfile;
    private Button bMyProfileSettings;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.my_profile_action_buttons_fragment, container, false);

        bMyProfileEditProfile = (Button) findViewById(R.id.bMyProfileEditProfile);
        bMyProfileSettings = (Button) findViewById(R.id.bMyProfileSettings);

        bMyProfileEditProfile.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent editUserProfile = new Intent (getActivity(), UserProfileEdit.class);
                startActivity(editUserProfile);

            }
        });
        bMyProfileSettings.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent settings = new Intent (getActivity(), Settings.class);
                startActivity(settings);

            }
        });
    }

变体2:MyProfileActionButtonsFragment类型的onCreateView(LayoutInflater,ViewGroup,Bundle)方法必须覆盖或实现超类型方法

public class MyProfileActionButtonsFragment extends SherlockFragmentActivity {
    private Button bMyProfileEditProfile;
    private Button bMyProfileSettings;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.my_profile_action_buttons_fragment, container, false);

        bMyProfileEditProfile = (Button) findViewById(R.id.bMyProfileEditProfile);
        bMyProfileSettings = (Button) findViewById(R.id.bMyProfileSettings);

1 个答案:

答案 0 :(得分:2)

尝试以下

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.my_profile_action_buttons_fragment, container, false);
bMyProfileEditProfile = (Button) view.findViewById(R.id.bMyProfileEditProfile);
bMyProfileSettings = (Button) view.findViewById(R.id.bMyProfileSettings);
bMyProfileEditProfile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent editUserProfile = new Intent (getActivity(), UserProfileEdit.class);
            startActivity(editUserProfile);
        }
    });
bMyProfileSettings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent settings = new Intent (getActivity(), Settings.class);
            startActivity(settings);
        }
    });
return view; 
}

删除

return inflater.inflate(R.layout.my_profile_action_buttons_fragment, container, false);

findViewById是一种活动类的方法,您需要使用膨胀的视图对象来初始化按钮,并且应该返回膨胀的视图。