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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:58:01+00:00 2026-06-11T14:58:01+00:00

I have tried many things, such as the 2 references: http://developer.android.com/training/basics/fragments/communicating.html & http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity along

  • 0

I have tried many things, such as the 2 references: http://developer.android.com/training/basics/fragments/communicating.html & http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity along with some help from others on StackOverflow and I just cannot get my ListFragment (FragmentA) to send just a simple string to another ListFragment (FragmentB). My app keeps crashing telling me there is a NullPointerException when I click the list in FragmentA going into FragmentB. I have been stuck on this for 2 days now and I just feel like smashing my head against a wall. If anyone knows what is wrong please post code to fix my problem.

MainActivity:

import com.example.fragmentcommunication.FragmentB.OnDataPass;

public class MainActivity extends Activity implements OnDataPass{
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
@Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            String librarySelected = libraryList[position];
            Fragment newFragment = null;

            if (librarySelected.equals("Item1")){
                newFragment = new FragmentA();

            }

            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.tab2, newFragment, "FragA");
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            transaction.commit();

        }

    });        
}

public void onDataPass(String data) {
    // TODO Auto-generated method stub

    FragmentB transaction1 = ((FragmentB) getFragmentManager().findFragmentByTag("FragB"));
    transaction1.use(data);
}
}

FragmentA:

public class FragmentA extends ListFragment{

... 
OnDataPass dataPasser;

public interface OnDataPass{
    public void onDataPass(String data);
}

@Override
public void onAttach(Activity a) {
    super.onAttach(a);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        dataPasser = (OnDataPass) a;
    } catch (ClassCastException e) {
        throw new ClassCastException(a.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}


@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String libraryList;

    //Get the position the user clicked.
    Fragment newFragment = null;

    libraryList = l.getItemAtPosition(position).toString();
    dataPasser.onDataPass(libraryList);

    if(libraryList.equals("Ranged")){
        newFragment = new FragmentB();          

    }

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.tab2, newFragment, "FragB");
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
}

}

FragmentB:

public class FragmentB extends ListFragment{

...
String getListInfo;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    //Testing to see if the String was passed from Fragment A.
    System.out.println(getListInfo);

    ...

public void use(String data) {
    // TODO Auto-generated method stub
    this.getListInfo = data;

}   
}
  • 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-11T14:58:02+00:00Added an answer on June 11, 2026 at 2:58 pm

    I just cannot get my ListFragment (FragmentA) to send just a simple string to another ListFragment (FragmentB).

    Since the primary point behind fragments is that they might not both exist on the screen at the same time (e.g., on -normal screens), FragmentA should not be trying to “send just a simple string to” FragmentB. FragmentB may not exist.

    If an event occurs in FragmentA that may have UI effects beyond FragmentA, FragmentA should tell its hosting activity about the event. The hosting activity can then route the event to the other fragment, either by directly calling a method on it (if the activity hosts FragmentB as well), creating it and adding it to the UI (and passing in the data using a factory method, like newInstance()), or by calling startActivity() to launch another activity (if there is insufficient room for FragmentA and FragmentB, and therefore we have a separate activity showing FragmentB).

    With respect to your existing code, getListInfo is null because you never assign any value to it.

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

Sidebar

Related Questions

I have tried so many things, cannot figure this out, I am using this
I have tried many permutations but they all don't seems to work well. Am
I have tried many options mentioned in the previous messages/forums.None of them seem to
Hello friends I have tried many times but I am not successful than please
This should be really simple, but I have tried many solutions posted on the
I want to get the page rank for any website. I have tried many
I'm trying to add values from array to DB, have tried many variuos examples
Yes, I have searched and tried many techniques, but nothing seems to work. Here
I am using jQuery fullcalendar plugin , I have tried altering this many different
I tried many ways to solve this problem but none works. suppose I have

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.