0

我有一个里面有两个按钮的片段。我无法将 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);
4

1 回答 1

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是一个活动类的方法,您需要使用膨胀的视图对象来初始化您的按钮,并且您应该返回膨胀的视图。

于 2013-10-28T12:17:16.960 回答