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 8409729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:59:26+00:00 2026-06-09T23:59:26+00:00

I’m sorry, I’m sure that this will be simple but I just cannot see

  • 0

I’m sorry, I’m sure that this will be simple but I just cannot see where I’m going wrong here.

I’ve got a Fragment (a tool list) that can start a number of other fragments (tools) in a two pane set up. There is an activity that sits above & I am using it to manage the removal of tool fragments when their ‘close button’ is pressed. It then pops a tool from the backstack if one exists.

The strange thing is, that I have this working within one fragment but the ‘cut & pasted’ code into another fragment doesn’t work & throws a NPE. Any ideas why much appreciated.

The Activity snippet:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;    

public class MyActivity extends FragmentActivity implements OnFragmentButtonClickedListener {

private final static int CLOSE_BUTTON = 0;
...


public void onFragmentButtonClicked(int button, Fragment fragId) {
    Log.i(TAG, "onFragmentButton");
    // process closing of fragment
    if (button == CLOSE_BUTTON) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.remove(fragId);
        ft.commit();
    }
    // then restore most recent fragment from backstack
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
    }
}
}

This fragment works:

import android.support.v4.app.Fragment;

public class FirstFragment extends Fragment implements OnClickListener{

OnFragmentButtonClickedListener fragmentButtonListener;
private final static int CLOSE_BUTTON = 0;


// empty constructor
public FirstFragment() {};

// Activity must implement this interface
public interface OnFragmentButtonClickedListener {
    public void onFragmentButtonClicked (int button, Fragment fragId);
}

@Override
public void onAttach(Activity activity) {
    Log.i(TAG, "onAttach");
    super.onAttach(activity);
    try {
        fragmentButtonListener = (OnFragmentButtonClickedListener) activity;
    } catch (ClassCastException e) {
        Log.w(TAG, (activity.toString() + " must implement OnFragmentButtonClickedListener"));
    }
}

...

public void onClick(View v) {
    Log.i(TAG, "onClick");
    switch (v.getId()) {
    case R.id.btn_st_close:
        //close activity
        eraseData(prefs);
        Log.i(TAG, "Frag Id = " + String.valueOf(getFragmentManager().findFragmentById(getId())));
        fragmentButtonListener.onFragmentButtonClicked(CLOSE_BUTTON, getFragmentManager().findFragmentById(getId()));
        break;
    }
}

But, this one throws a NPE in the onClick method when calling the onFragmentButtonClicked() method on the fragmentButtonListener.

import android.support.v4.app.Fragment;

public class SecondFragment extends Fragment implements OnClickListener {

OnFragmentButtonClickedListener fragmentButtonListener;
private final static int CLOSE_BUTTON = 0;

// empty constructor
public SecondFragment() {};

// Activity must implement this interface
public interface OnFragmentButtonClickedListener {
    public void onFragmentButtonClicked (int button, Fragment fragId);
}

@Override
public void onAttach(Activity activity) {
    Log.i(TAG, "onAttach");
    super.onAttach(activity);
    try {
        fragmentButtonListener = (OnFragmentButtonClickedListener) activity;
    } catch (ClassCastException e) {
        Log.w(TAG, (activity.toString() + " must implement OnFragmentButtonClickedListener"));
    }
}

...

 public void onClick(View v) {
     Log.i(TAG, "OnClick");
    switch (v.getId()) {
    case R.id.btn_wc_close:
        //close activity
        eraseData(prefs);
        Log.i(TAG, "Frag Id = " + String.valueOf(getFragmentManager().findFragmentById(getId())));
        try {
            fragmentButtonListener.onFragmentButtonClicked(CLOSE_BUTTON, getFragmentManager().findFragmentById(getId()));
        }
        catch (Exception e) {
            Log.i(TAG, "Exception: " + e);
        }
        break;
        }
    }

From logcat, I can see that the fragment Id is not null so I’m very confused.

08-20 09:13:49.096: I/Second Fragment(1691): onCreate
08-20 09:13:49.096: I/Second Fragment(1691): Frag Id = SecondFragment{41355d68 #1 id=0x7f090093}
08-20 09:13:49.106: I/Second Fragment(1691): onCreateView
08-20 09:13:49.406: I/Second Fragment(1691): onActivityCreated
08-20 09:13:49.560: I/Second Fragment(1691): onClick
0 09:13:51.766: I/Second Fragment(1691): Frag Id = SecondFragment{41355d68 #1 id=0x7f090093}
08-20 09:13:51.766: W/dalvikvm(1691): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-20 09:13:51.826: E/AndroidRuntime(1691): FATAL EXCEPTION: main
08-20 09:13:51.826: E/AndroidRuntime(1691): java.lang.NullPointerException
08-20 09:13:51.826: E/AndroidRuntime(1691):     at SecondFragment.onClick(IceTurbFragment.java:136)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at android.view.View.performClick(View.java:4084)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at android.view.View$PerformClick.run(View.java:16966)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at android.os.Handler.handleCallback(Handler.java:615)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at android.os.Looper.loop(Looper.java:137)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at java.lang.reflect.Method.invoke(Method.java:511)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-20 09:13:51.826: E/AndroidRuntime(1691):     at dalvik.system.NativeStart.main(Native Method)

Thanks for any help.

  • 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-09T23:59:27+00:00Added an answer on June 9, 2026 at 11:59 pm

    I can see you define 2 interface with the same name in each fragment.

    public interface OnFragmentButtonClickedListener {
         public void onFragmentButtonClicked (int button, Fragment fragId);
    }
    

    But the activity can implement only one interface with this name (in this case, I believe your activity implement interface of first fragment) and maybe this cause error null pointer exception of second fragment.

    //where is OnFragmentButtonClickedListener come from?
    public class MyActivity extends FragmentActivity implements FirstFragment OnFragmentButtonClickedListener 
    

    Try to write OnFragmentButtonClickedListener outside of 2 fragments ( maybe in another file)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

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.