Skip to content Skip to sidebar Skip to footer

Nullpointerexception With Settext In A Fragment (android)

I have an app with separate fragments using scroll tabs to go between. Each fragment just displays preset text. The text is going to be formatted for good appearance though so I ne

Solution 1:

It would be easier to find errors if you provide the Stacktrace from LogCat, but I see one obvious error:

In your Fragment you are referring to the Activity to get the TextView, but your textviewabout is sure a part of your fragment, so I am sure the code has to be

publicstaticclassAboutMeFragmentextendsFragment {

     publicAboutMeFragment() {
     }

     @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
          ViewrootView= inflater.inflate(R.layout.aboutme,
            container, false);
          textviewabout = (TextView)rootView.findViewById(R.id.textviewabout);
          textviewabout.setText(Html.fromHtml(getString(R.string.about_me)));
          return rootView;
     }
}

Edit: Further in your activity you only show the declarations, no code how you want to show the Fragment. You should add the XML of your Activity to show if the Fragment is declared there.

Solution 2:

You should initialize views and call setText in onActivityCreated of your Fragment. Because there might be a possibility that your views are not ready yet.

Solution 3:

It seems to me that your textviewabout object is null (maybe you can confirm this by debugging your application).

How are you telling your Activity to use the aboutme.xml as a layout? Do you really intend to use this layout for the activity or do you want to use it for the fragment ui?

If you want to use it for the fragment you have to use the inflater you get in the onCreateView method to inflate your aboutme.xml layout first and then retrieve the TextView afterwards (and you might want to have the TextView inside the Fragment class, there should be no need for a static view class in any normal szenario)

Regards, Thomas

Post a Comment for "Nullpointerexception With Settext In A Fragment (android)"