Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:39:52+00:00 2026-06-14T07:39:52+00:00

In my android application, I have a fragment-fragment1. Inside that fragment, I have a

  • 0

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

    }




}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:39:54+00:00Added an answer on June 14, 2026 at 7:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my android application Iam using viewpager.I have two fragments.Fragment1 and Fragment2.Iam calling two
In an Android application I have a fragment implemented that overrides onViewCreated to set
When creating an Android application using Loaders, should every activity and fragment have its
I have a SeachView inside of a Fragment in an Android 3.1 application currently
I am using the XMPP Connection(using smack) for chat in android application.I have made
I'm developing a simple Android application that would have an action bar on the
I'm developing an Android 3.1 tablet application and I have a Fragment ( android.support.v4.app.Fragment
In my Android application, I have a fragment activity which it has a progressbar.
I'm using fragments in my Android application using the compatibility package. I have tested
I'm developing a tab based android application,I have seen that TabActivity class is deprecated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.