Basim Sherif Basim Sherif - 3 months ago 47x
Java Question

findViewById not working in a static function inside fragment

In my android application, I have a fragment-fragment1. Inside that fragment, I have a static function 'function1'.I tried to define a buttton inside that static function using,


But Eclipse throws an error like "Cannot make a static reference to the non-static method getActivity() from the type Fragment".What am I done wrong? I need this function1 to be static, so that I can call it from another class.I mean, I have to populate the list view from main activity when I select a particular fragment.


public class Fragment1 extends Fragment{

public static String feedurl="";
static String URL = "";
static final String KEY_HEAD = "item"; // parent node
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
static Button button;
HeadlinesAdapter adapter;
private TextView mMessageView;
private Button mClearButton;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.first_fragment, container, false);

return v;

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);



public static void function1()

ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_HEAD);
NodeList itemLst = doc.getElementsByTagName("item");
String MarqueeStr="";

for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);

button=(Buton)getActivity().findViewById(;;// Here it shows error.
adapter=new Adapter1(getActivity(), newsList);



And my mainactivity is,


public class ViewPagerActivity extends FragmentActivity {

private ViewPager mViewPager;
private MessageLoader mLoader;
private Button mSenderButton, mReceiverButton;

public void onCreate(Bundle savedInstanceState) {
// We get UI references
mViewPager = (ViewPager) findViewById(;
mSenderButton = (Button) findViewById(;
mReceiverButton = (Button) findViewById(;
// We set pager adapter
mViewPager.setAdapter(new MyAdapter(this));
// We set receiver button listener

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)


int i=0;
public void onPageSelected(int position) {

}else if(position==1 && i==0){
Fragment1.function1(); // Here Iam calling function1



public void onPageScrollStateChanged(int state) {


mReceiverButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// We set sender button listener
mSenderButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


* Adapter for ViewPAger that manages background interactions with fragments
private class MyAdapter extends FragmentPagerAdapter{

private Context mContext;
private String[] frags = {Headlines.class.getName(), Kerala.class.getName()};

public MyAdapter(FragmentActivity activity) {
mContext = activity;

public Object instantiateItem(ViewGroup container, int position) {
Fragment frag = (Fragment) super.instantiateItem(container, position);
if(frag instanceof MessageLoader){
mLoader = (MessageLoader) frag;
return frag;

public Fragment getItem(int pos) {
return Fragment.instantiate(mContext, frags[pos]);

public int getCount() {
return frags.length;




Change the signature of function1() to be as:

public static void function1(Fragment f)

and use:

button = (Buton) f.getActivity().findViewById(;

instead of:

button = (Buton) getActivity().findViewById(;

and call the method like this:


However, this method should be instance method rather than static method if there is no purpose for it.