Basim Sherif Basim Sherif - 5 months ago 76
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,

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


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.

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 mainactivity is,

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;
}

}




}

Answer

Change the signature of function1() to be as:

public static void function1(Fragment f)

and use:

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

instead of:

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

and call the method like this:

function1(this);

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