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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:17:01+00:00 2026-06-12T18:17:01+00:00

When Im trying to pass a parameter from FragmentActivity to a Fragment it gives

  • 0

When Im trying to pass a parameter from FragmentActivity to a Fragment it gives me null pointer exception in the getArguments() in the Fragment.

Here is my FragmentActivity Code

  public class IndexChartActivity extends FragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.index_chart);

            IndexFragmentActivity indexFragment =   
 (IndexFragmentActivity)getSupportFragmentManager().findFragmentById(R.id.index_fragment);
            indexFragment.newInstance("ASPI");

        }

    }

Here is the index_chart.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/header_fragment"
        android:name="com.lk.ignitionit.cse.util.HeaderFragmentActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/index_fragment"
        android:name="com.lk.ignitionit.cse.util.IndexFragmentActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment
        android:name="com.lk.ignitionit.cse.util.ChartFragmentActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

and Here is the Fragment

public class IndexFragmentActivity extends Fragment {
    protected ImageView ivASPI;
    protected ImageView ivMPI;
    protected ImageView ivSP;
    protected TextView tvMain;
    protected TextView tvTop;
    protected TextView tvBottom;
    String response = null;
    String result = null;
    String []  resultArr = null;
    Bundle b = new Bundle();
    String indexType = null;
    int layout;
    IndexFragmentActivity f = null;

    public  IndexFragmentActivity newInstance(String index) {
        f = new IndexFragmentActivity();
        Bundle args = new Bundle();
        args.putString("indextype", index);
        f.setArguments(args);
        return f;
    }

    public String getSelectedIndex() {
        return f.getArguments().getString("indextype");
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(getSelectedIndex().equals("ASPI")){
            layout = R.layout.aspi_header;
        }else if(getSelectedIndex().equals("MPI")){
            layout = R.layout.mpi_header;
        }else{
            layout = R.layout.sp_header;
        }
        View view = inflater.inflate(layout, container, false);

        tvMain = (TextView) view.findViewById(R.id.tv_main);
        tvTop = (TextView) view.findViewById(R.id.tv_top);
        tvBottom = (TextView) view.findViewById(R.id.tv_bottom);


        new ServiceAccess().execute("");

        tvMain.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                b.putString("type", "S&P SL20");
                Activity activity = getActivity();
                Intent intent = new Intent(activity, IndexChartActivity.class);
                intent.putExtras(b);
                startActivity(intent);  

            }
        });

        return view;
    }
}

and Here is my ERROR

Caused by: java.lang.NullPointerException
at com.lk.ignitionit.cse.util.IndexFragmentActivity.getSelectedIndex(IndexFragmentActivity.java:69)

I referred lots of stackoverflow questions relevant to this and tried the sample code given http://developer.android.com/guide/components/fragments.html as well. But still no luck

Really appreciate any feed back on this as this seems to be a very basic issue which I cannot figure out..

Thanks in Advance

  • 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-12T18:17:03+00:00Added an answer on June 12, 2026 at 6:17 pm
    1. Get rid of Activity from the names of all classes that do not inherit from Activity. For example,
      IndexFragmentActivity is not an Activity.

    2. In onCreate() of your actual activity, you are calling findFragmentById() to retrieve a fragment, then calling newInstance() on that fragment. However, the fragment will not exist the first time onCreate() is called, and you will fail here with a NullPointerException. Please correctly handle this case.

    3. The method name newInstance in Java is most commonly associated with the factory pattern, where newInstance() is a static method used in place of a public constructor to create instances of some class. Your newInstance() method is not static. This will cause confusion for those who come after you to maintain the code.

    4. You call newInstance() in onCreate(), create the new fragment instance, and then throw it away, which is a waste of time.

    Hence, assuming that your original instance of your fragment (wherever it came from) does not have an arguments Bundle set, that would explain your NullPointerException in getSelectedIndex().

    When Im trying to pass a parameter from FragmentActivity to a Fragment

    To pass a parameter to a newly created fragment, using a static newInstance() method and the arguments Bundle to create the new fragment is perfectly reasonable.

    To pass a parameter to a fragment that already exists, simply call some setter method on that fragment.

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

Sidebar

Related Questions

I'm trying to pass a list from one form class to another. Here's the
I am trying to pass a pointer by reference to an object from class
I'm trying to pass parameter from python page to another through the URL,this parameter(key
I am trying to pass a parameter from AJAX back to my JSP page.
I am trying to pass a character pointer array as an input from C
I'm trying to pass parameter of short type to C++ unmanaged function imported from
i trying to pass parameter from one site to the next site and use
i'm trying to pass a parameter from a activity Main, to the tabs of
I'm trying to pass a parameter from php into my javascript function inside html.
I am trying to pass the parameter constraint to PDO from an array i.e.

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.