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);
source share