Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

android - Fragment returning Null context

I've been trying to fix this for the past 2 hours. Tried a lot of things.

The Fragment seems to be passing a null context to my Adapter. I have tried to initialize the Context variable in onCreate and onCreateView and onActivityCreated. Same result.

Here is my fragment:

    public class ActiveBookingsFragment extends Fragment
{
    Context con;
    ArrayList<Booking> bookings;
    ListView activeBookings_lv;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        con = this.getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View v =inflater.inflate(R.layout.tab_activebookings,container,false);

        activeBookings_lv = (ListView) v.findViewById(R.id.lv_activebookings);
        bookings = new ArrayList<Booking>();

        Booking b1 = new Booking("Data");
        bookings.add(b1);

        activeBookings_lv.setAdapter(new BookingAdapter(con, bookings));

        return v;
    }
}

And here is the logcat error:

java.lang.NullPointerException
        at android.view.LayoutInflater.from(LayoutInflater.java:212)
        at com.nettechltd.cabeecustomer.CustomAdapters.BookingAdapter.<init>(BookingAdapter.java:26)
        at com.nettechltd.cabeecustomer.ActiveBookingsFragment.onCreateView(ActiveBookingsFragment.java:51)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1413)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:696)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:589)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5428)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
        at android.view.View.measure(View.java:17633)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2347)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1439)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1653)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1297)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6773)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
        at android.view.Choreographer.doCallbacks(Choreographer.java:613)
        at android.view.Choreographer.doFrame(Choreographer.java:583)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5748)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
        at com.android.internal.os.ZygoteIn
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Initialize context in Fragment onAttach(.....) like

 @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    con = getActivity();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...