简体   繁体   中英

Android ActionBarSherlock Tabs: Fragments View

I've got a problem with fragments and views on ActionBarSherlock:

I've successfully implemented 2 tabs and the tabs open my fragment view.

public class AFragment extends SherlockFragment {

private ImageButton imageButton1;
private ImageButton imageButton2;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment


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



}
}

This is my code, the layout shows a TextView and 2 ImageButtons. I want to declare the image button like imagebutton = (ImageButton) findViewById(R.id.imagebutton1); but that gives me an error at findViewById. I tried at every position inside my AFragment Activity but there's still the same error. I also tried to add a OnCreate function but this doesn't work either... So where do I need to put my code for that view to work? Thank you!

You should declare a variable that contains your view, do the declaration and modifiations you want, and then return the view

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.activity_afragment, container, false);
    ImageButton imageButton = (ImageButton) v.findViewById(R.id.imagebutton1); 
    return v;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM