Wednesday, 2 October 2013

method to append text in fragment causing null pointer

method to append text in fragment causing null pointer

pI am trying to change the text in the text view of a fragment on button
click of another fragment in the same activity./p ponCreateView is
returned at the top of the class/p precodepublic View
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) { View view =
inflater.inflate(R.layout.deckbuilder_fragment, container, false); return
view; } /code/pre pI have the TextView declared outside of any method
between the onCreateView and onStart/p precodeTextView deckSize; /code/pre
pthe same code works fine in the onStart method between onCreateView and
deckSize(). /p precodepublic 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 pbut then the null pointer happens here at line 180/p
precodepublic 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 pThis 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 precodeFATAL 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 pLine 180 is this
line/p precodedeckSize = (TextView) getView().findViewById(R.id.decksize);
/code/pre pI am calling on this deckSize method from a method in the
parent activity/p precodepublic void deckSize() { DeckBuilderFragment
deckBuilderFragment = new DeckBuilderFragment();
deckBuilderFragment.deckSize(); } /code/pre pand I am calling on that
method in the parent activity from another fragment that resides in the
same parent activity like this/p precode((DeckBuilder)
getActivity()).deckSize(); /code/pre pI am still fairly new to Android and
Java and would really appreciate any help or guidance that anyone can
give./p

No comments:

Post a Comment