findViewById не распознан как метод внутри SherlockFragment
У меня есть фрагмент с двумя кнопками внутри. Я не могу прикрепить кнопки 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: метод onCreateView(LayoutInflater, ViewGroup, Bundle) типа MyProfileActionButtonsFragment должен переопределять или реализовывать метод супертипа
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 ответ
Решение
Попробуйте ниже
@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
это метод класса деятельности, вам нужно использовать объект завышенного представления для инициализации ваших кнопок, и вы должны возвратить завышенное представление.