2013-10-28 2 views
0

У меня есть фрагмент с двумя кнопками внутри. Я не могу прикрепить кнопки xml к кнопкам java, потому что в классе SherlockFragment findViewById не распознается как метод.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, Пачка) типа 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); 

ответ

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 является методом класса активности необходимо использовать завышенный объект представления инициализировать кнопки, и вы должны вернуть надутый вид.

+0

что не так? почему это вниз? – Raghunandan

+0

Огромное вам спасибо –

+0

@ J.Kowalski приветствую вас и рад, что это помогло, но какой-то пользователь отказался без причины, и я не вижу ничего плохого в ответе – Raghunandan

Смежные вопросы