Reputation: 5440
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;
}
}
}
Upvotes: 6
Views: 9193
Reputation: 1
You must inflate that particular layout an use that layout to find the id.
**
View v = inflater.inflate(R.layout.first_fragment, container, false);
button=(Button) v.findViewById(R.id.button1);
return v;
**
Upvotes: 0
Reputation: 6553
@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;
}
Upvotes: 0
Reputation: 2562
You currently have:
static Button button;
You should remove the static keyword, and instead write:
private Button button;
Upvotes: 0
Reputation: 117655
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.
Upvotes: 5
Reputation: 28561
There is no reason here for anything to be static (apart from the constants you put in CAPITALS that should be both declared static
and final
).
Remove all the static
keywords from your code and it should work (apart from the CAPITALIZED variables):
private Button button;
// ...
public void function1() {
// ...
}
Upvotes: 0
Reputation: 68187
Don't use static method as it cannot refer to Fragment1
instance unless you provide it as input parameter. Besides change the finding of button as below:
button = (Buton) getView().findViewById(R.id.button1);
Upvotes: 3