0

well i am doing a small android project involving fragments.. my log cat is showing null pointer exception as context value is null..

this is my code

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.syllabusselection, container, false);
    main(rootView);
    return rootView;
}
public void main(View rootView)
 {
     TextView textyear,textsubject,textschema,textPowered ;
        final Context context = getActivity();
        Typeface Roboto = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Light.ttf");
        Typeface Nexa = Typeface.createFromAsset(context.getAssets(),"fonts/nexa/Nexa_Light.ttf");//Nexa_Light.ttf Nexa_Bold.ttf


        textsubject = (TextView)rootView.findViewById(R.id.textsubject);
        textschema = (TextView)rootView.findViewById(R.id.textscheme);
        textPowered = (TextView)rootView.findViewById(R.id.textPowered);

well originally the

final Context context = getActivity();

was null i changed it to getActivity() but still i am getting null pointer excewption.. so what is the errror??

this is my log cat

03-10 03:30:06.015: E/AndroidRuntime(1218): FATAL EXCEPTION: main
03-10 03:30:06.015: E/AndroidRuntime(1218): Process: info.androidhive.tabsswipe, PID: 1218
03-10 03:30:06.015: E/AndroidRuntime(1218): java.lang.NullPointerException
03-10 03:30:06.015: E/AndroidRuntime(1218):     at info.androidhive.tabsswipe.Question.main(Question.java:63)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at info.androidhive.tabsswipe.Question.onCreateView(Question.java:55)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.View.measure(View.java:16497)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.Choreographer.doFrame(Choreographer.java:544)
03-10 03:30:06.015: E/AndroidRuntime(1218):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)

2 Answers 2

3

Move your invocation of main to onActivityCreated of the fragment. That means you should move the rootView.findViewById calls to onCreateView and retain the Typeface calls in main.

Sign up to request clarification or add additional context in comments.

Comments

0

I guess that when you try to get context, fragment is not attached to Activity yet. So you should rethink app logic. As pointed by @Vino you should use another callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.