Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing parameters from Android FragmentActivity to Fragment
    primarykey
    data
    text
    <p>When Im trying to pass a parameter from FragmentActivity to a Fragment it gives me null pointer exception in the getArguments() in the Fragment.</p> <p>Here is my FragmentActivity Code</p> <pre><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"); } } </code></pre> <p>Here is the index_chart.xml</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;fragment android:id="@+id/header_fragment" android:name="com.lk.ignitionit.cse.util.HeaderFragmentActivity" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;fragment android:id="@+id/index_fragment" android:name="com.lk.ignitionit.cse.util.IndexFragmentActivity" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;fragment android:name="com.lk.ignitionit.cse.util.ChartFragmentActivity" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>and Here is the Fragment</p> <pre><code>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&amp;P SL20"); Activity activity = getActivity(); Intent intent = new Intent(activity, IndexChartActivity.class); intent.putExtras(b); startActivity(intent); } }); return view; } } </code></pre> <p>and Here is my ERROR</p> <pre><code>Caused by: java.lang.NullPointerException at com.lk.ignitionit.cse.util.IndexFragmentActivity.getSelectedIndex(IndexFragmentActivity.java:69) </code></pre> <p>I referred lots of stackoverflow questions relevant to this and tried the sample code given <a href="http://developer.android.com/guide/components/fragments.html" rel="nofollow">http://developer.android.com/guide/components/fragments.html</a> as well. But still no luck</p> <p>Really appreciate any feed back on this as this seems to be a very basic issue which I cannot figure out..</p> <p>Thanks in Advance </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload