Passing parameters from Android FragmentActivity to fragment

When I try to pass a parameter from FragmentActivity to a fragment, it gives me a null pointer exception in 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 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 a 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 mentioned a lot of stackoverflow questions related to this and tried the code example provided by http://developer.android.com/guide/components/fragments.html . But still no luck

Really appreciate any feedback as this is apparently a very simple problem that I cannot understand.

Thanks at Advance

+4
source share
1 answer
  • Get rid of Activity from the names of all classes that are not inherited from Activity . For example, IndexFragmentActivity not an Activity .

  • In onCreate() your actual activity, you call findFragmentById() to retrieve the fragment, and then call newInstance() on that fragment. However, the fragment will not exist on the first call to onCreate() , and you are not here with a NullPointerException . Please handle this case correctly.

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

  • You call newInstance() in onCreate() , instantiate a new fragment, and then throw it away, which is a waste of time.

Therefore, assuming that your original instance of your fragment (wherever it appears) has no Bundle set arguments, this explains your NullPointerException in getSelectedIndex() .

When I try to pass a parameter from FragmentActivity to a fragment

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

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

+18
source

Source: https://habr.com/ru/post/1439547/


All Articles