当前位置: 首页 > 知识库问答 >
问题:

片段返回空上下文

梁勇
2023-03-14

我在过去的两个小时里一直在试图解决这个问题。试过很多东西。

该片段似乎正在向我的适配器传递空上下文。我尝试在onCreate和onCreateView和onActivityCreated中初始化上下文变量。同样的结果。

    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;
    }
}
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

共有1个答案

柴正祥
2023-03-14

初始化片段onAttach(...)中的上下文

 @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    con = getActivity();
    }
}
 类似资料:
  • 我在使用FragmentPagerAdapter时遇到问题。我想从视图寻呼机中的片段中检索数据。当我调用getItem()方法时,它返回一个片段,但不创建片段。所有片段变量均为null。我需要做什么来实例化片段?这是我的适配器。 这是viewpager中的片段。 我不太了解ViewPager是如何工作的,但据我所知,片段在没有设置任何变量的情况下返回的原因是因为ViewPager没有附加片段。如何

  • 问题内容: 我有一个关于片段中上下文使用情况的问题。我的问题是,我总是收到NullpointerException。这是我的工作: 创建一个扩展SherlockFragment的类。在该类中,我有另一个Helper类的实例: 这是其他Helper类的摘录: 每次调用时(例如context.getResources()),我都会收到NullPointerException。这是为什么? 问题答案:

  • 该项目由两个包组成,我是捆绑在一起在一个单一的模块。 “main”类(入口点)是。 下面是类的内容:

  • 我正在调用tabhost片段活动中的映射片段

  • 在Android中,当我们用一个新的片段替换容器视图时,我们可以使用replace()和addToBackStack(),因此按下back按钮就可以转到上一个片段。 但如果出现以下情况怎么办: 在Activity1中,我可以按下back按钮从fragment2转到fragment1。但当fragment2启动另一个活动时,从Activity2按下back按钮,它会将我带到Activity1中的fr

  • 我想创建带有PagerView的片段,但运行以下代码会返回一个IllegalStateException: 源代码: IllegalStateException: 进程:com.company.user.blabla,pid:22256 java.lang.illegalstate异常:带有适配器的ViewPager com.company.user.blabla.fragments.homeFr