FindViewById does not work in static function inside fragment

In my Android app, I have snippet fragment1. Inside this snippet, I have a static function 'function1'. I tried to define that point inside this static function using

button=(Buton)getActivity().findViewById(R.id.button1); 

But Eclipse throws an error like โ€œIt is not possible to make a static reference to the non-static getActivity () method from theโ€œ Fragment โ€type. What did I do wrong? I need this function1 to be static, so I can call it from another class. I mean, that I have to populate the list view from the main action when I select a specific fragment.

Fragment1 =>

 public class Fragment1 extends Fragment{ public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml"; 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; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.first_fragment, container, false); return v; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); function1(); populate_listview(); } public static void function1() { URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml"; 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); newsList.add(map); } button=(Buton)getActivity().findViewById(R.id.button1);;// Here it shows error. adapter=new Adapter1(getActivity(), newsList); list.setAdapter(adapter); } 

}

And my main activity:

ViewpagerActivity =>

 public class ViewPagerActivity extends FragmentActivity { private ViewPager mViewPager; private MessageLoader mLoader; private Button mSenderButton, mReceiverButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // We get UI references mViewPager = (ViewPager) findViewById(R.id.viewPager); mSenderButton = (Button) findViewById(R.id.sender_button); mReceiverButton = (Button) findViewById(R.id.receiver_button); // 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) { if(position==0){ }else if(position==1 && i==0){ Fragment1.function1(); // Here Iam calling function1 i++; } } public void onPageScrollStateChanged(int state) { } }); mReceiverButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mViewPager.setCurrentItem(0); } }); // We set sender button listener mSenderButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mViewPager.setCurrentItem(1); } }); } /** * 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) { super(activity.getSupportFragmentManager()); mContext = activity; } @Override public Object instantiateItem(ViewGroup container, int position) { Fragment frag = (Fragment) super.instantiateItem(container, position); if(frag instanceof MessageLoader){ mLoader = (MessageLoader) frag; } return frag; } @Override public Fragment getItem(int pos) { return Fragment.instantiate(mContext, frags[pos]); } @Override public int getCount() { return frags.length; } } } 
+6
source share
6 answers

Change the signature of function1() as:

 public static void function1(Fragment f) 

and use:

 button = (Buton) f.getActivity().findViewById(R.id.button1); 

instead:

 button = (Buton) getActivity().findViewById(R.id.button1); 

and call the method as follows:

 function1(this); 

However, this method should be an instance method, not a static method if there is no purpose for it.

+5
source

Do not use the static method as it cannot refer to an instance of Fragment1 unless you provide it as an input parameter. Also, change the button search as shown below:

 button = (Buton) getView().findViewById(R.id.button1); 
+2
source

You currently have:

 static Button button; 

You should remove the static keyword and write instead:

 private Button button; 
0
source

There is no reason for static (besides the constants that you insert in CAPITALS, which should be declared as static and final ).

Remove all static keywords from your code and it should work (except for the CAPITALIZED variables):

 private Button button; // ... public void function1() { // ... } 
0
source

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

 View v = inflater.inflate(R.layout.first_fragment, container, false); button=(Button) v.findViewById(R.id.button1); return v; 

}

0
source

You must inflate this particular layout using this layout to find the identifier.

**

 View v = inflater.inflate(R.layout.first_fragment, container, false); button=(Button) v.findViewById(R.id.button1); return v; 

**

0
source

All Articles