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

  • SEARCH
  • Home
  • 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 8255439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:31:27+00:00 2026-06-08T01:31:27+00:00

I have a Fragment with a Button embedded inside of a FragmentActivity. When I

  • 0

I have a Fragment with a Button embedded inside of a FragmentActivity. When I click the button I want the Fragment to be replaced with another Fragment. The problem is: the Fragment is a inner static class of my Activity and the method is a non-static one. I solved this problem by making an instance of my Activity class, but when I click on the button inside the fragment the application crashes.

Here is the code:

public class Stdp extends SherlockFragmentActivity implements ActionBar.OnNavigationListener {

public static class Bottomfrag extends SherlockFragment {

    static Bottomfrag newInstance() {
        Bottomfrag f = new Bottomfrag();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_choose, container, false);
        View li = v.findViewById(R.id.layoutbottom);
        li.setBackgroundColor(Color.parseColor("#FFBB33"));
        View button = v.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Stdp t = new Stdp();
                t.addFragmentToStack();
            }
        });
        return v;
    }
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stdp);

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.test_array, R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);


    if (savedInstanceState == null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment bottom = new Bottomfrag();
        ft.add(R.id.su1, bottom);
        ft.commit();
    }
}


@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    // TODO Auto-generated method stub
    return false;
}

public void addFragmentToStack() {
Fragment newFragment = Bottomfrag.newInstance();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.su1, newFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
    ft.addToBackStack(null);
    ft.commit();
}
}

I found a solution for my problem. The only thing I had to do is to move the addFragmentToStack to the Bottomfrag class.

  • 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-08T01:31:29+00:00Added an answer on June 8, 2026 at 1:31 am

    I solved this problem by making an instance of my Activity class.

    Instantiating an Activity is almost never a solution when it comes to Android development… in fact, I can’t imagine a scenario in which you would ever want to create a new Activity using the default constructor.

    You can reference the static instance of your Activity with ActivityName.this.

    You should also move addFragmentToStack to the Bottomfrag class.

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

Sidebar

Related Questions

Inside a fragment, I have a button which I want to trigger the filling
I have two Fragment s in my Activity : fragment A with button X
i have a button called ac and when i click on it, i want
I have a parent FragmentActivity with three child Tab Fragments. When a Submit button
I have fragment that on a component click pop-ups DialogFragment. This dialog fragment holds
I have a fragment that I need to display on the screen. I want
I have a fragment where I wish to call a method from the FragmentActivity
I have defined in XAML a list view, see following fragment: <Grid> <Button Content=_Generate
I have a problem with my android-app. I want to save a Data-list in
I have a fragment with some buttons in it, when a button is clicked

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.