过去2小时我一直试图解决这个问题。尝试了很多事情。
Fragment似乎是将一个空上下文传递给我的Adapter。我试图在onCreate和onCreateView以及onActivityCreated中初始化Context变量。结果相同。
这是我的片段:
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;
}
}
这是logcat错误:
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
答案 0 :(得分:1)
在context
中初始化Fragment onAttach(.....)
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
con = getActivity();
}
}
答案 1 :(得分:0)
你可以随时调用getActivity()来获取fragement中的上下文
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View v =inflater.inflate(R.layout.tab_activebookings,container,false);
...
activeBookings_lv.setAdapter(new BookingAdapter(getActivity(), bookings));
return v;
}
}
答案 2 :(得分:0)
onActivityCreated()
之后调用 onCreateView()
。要获取上下文只需使用getActivity()
,请不要创建另一个引用活动的字段(以后可能会导致内存泄漏)。
无论如何,您需要此字段,您可以在onAttach()
初始化它。