Note that there are some explanatory texts on larger screens.

plurals
  1. POmethod to append text in fragment causing null pointer
    primarykey
    data
    text
    <p>I am trying to change the text in the text view of a fragment on button click of another fragment in the same activity.</p> <p>onCreateView is returned at the top of the class</p> <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.deckbuilder_fragment, container, false); return view; } </code></pre> <p>I have the TextView declared outside of any method between the onCreateView and onStart</p> <pre><code>TextView deckSize; </code></pre> <p>the same code works fine in the onStart method between onCreateView and deckSize(). </p> <pre><code>public void onStart() { super.onStart(); deckSize = (TextView) getView().findViewById(R.id.decksize); final SharedPreferences defaultDeck = getActivity() .getSharedPreferences("defaultDeck", Context.MODE_PRIVATE); deckSize.setText(String.valueOf(defaultDeck.getInt("decksize", 0))); </code></pre> <p>}</p> <p>but then the null pointer happens here at line 180</p> <pre><code>public void deckSize() { deckSize = (TextView) getView().findViewById(R.id.decksize); //line 180 final SharedPreferences defaultDeck = getActivity() .getSharedPreferences("defaultDeck", Context.MODE_PRIVATE); deckSize.setText(String.valueOf(defaultDeck.getInt("decksize", 0))); } </code></pre> <p>This is the method coded into the fragment in which the TextView resides in. I have the TextView built in the XML layout file for this fragment. Strangely, this exact code that is inside this method is also in the onStart method of the same fragment further up in the class tree and it works fine. I declared the TextView deckSize further up in the class before the onStart method. Here is the logcat text that is relevant.</p> <pre><code>FATAL EXCEPTION: main java.lang.NullPointerException at com.maliciousgames.deckbuilder.DeckBuilderFragment.deckSize(DeckBuilderFragment.java:180) at com.maliciousgames.deckbuilder.DeckBuilder.deckSize(DeckBuilder.java:35) at com.maliciousgames.deckbuilder.BlockCardBrowserFragment$2.onClick(BlockCardBrowserFragment.java:105) at android.view.View.performClick(View.java:4240) at android.view.View$PerformClick.run(View.java:17721) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>Line 180 is this line</p> <pre><code>deckSize = (TextView) getView().findViewById(R.id.decksize); </code></pre> <p>I am calling on this deckSize method from a method in the parent activity</p> <pre><code>public void deckSize() { DeckBuilderFragment deckBuilderFragment = new DeckBuilderFragment(); deckBuilderFragment.deckSize(); } </code></pre> <p>and I am calling on that method in the parent activity from another fragment that resides in the same parent activity like this</p> <pre><code>((DeckBuilder) getActivity()).deckSize(); </code></pre> <p>I am still fairly new to Android and Java and would really appreciate any help or guidance that anyone can give.</p>
    singulars
    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.
    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