Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>Get rid of <code>Activity</code> from the names of all classes that do not inherit from <code>Activity</code>. For example, <code>IndexFragmentActivity</code> is not an <code>Activity</code>.</p></li> <li><p>In <code>onCreate()</code> of your actual activity, you are calling <code>findFragmentById()</code> to retrieve a fragment, then calling <code>newInstance()</code> on that fragment. However, the fragment will not exist the first time <code>onCreate()</code> is called, and you will fail here with a <code>NullPointerException</code>. Please correctly handle this case.</p></li> <li><p>The method name <code>newInstance</code> in Java is most commonly associated with the <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="noreferrer">factory pattern</a>, where <code>newInstance()</code> is a <code>static</code> method used in place of a public constructor to create instances of some class. Your <code>newInstance()</code> method is not <code>static</code>. This will cause confusion for those who come after you to maintain the code.</p></li> <li><p>You call <code>newInstance()</code> in <code>onCreate()</code>, create the new fragment instance, and then throw it away, which is a waste of time.</p></li> </ol> <p>Hence, assuming that your original instance of your fragment (wherever it came from) does not have an arguments <code>Bundle</code> set, that would explain your <code>NullPointerException</code> in <code>getSelectedIndex()</code>.</p> <blockquote> <p>When Im trying to pass a parameter from FragmentActivity to a Fragment</p> </blockquote> <p>To pass a parameter to a <em>newly created</em> fragment, using a <code>static</code> <code>newInstance()</code> method and the arguments <code>Bundle</code> to create the <em>new</em> fragment is perfectly reasonable.</p> <p>To pass a parameter to a fragment that already exists, simply call some setter method on that fragment.</p>
 

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