我已经编写了一个自定义视图,用于在24小时垂直滚动时间轴中显示事件。
在扩展此视图的片段中的onCreateView方法中,我正在给视图充气,然后尝试设置视图公开的回调。但是,当我尝试在自定义视图上设置属性时,我得到一个空引用异常(即自定义视图为null)。
这是我的布局(在约束布局中使用我的自定义DayView):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".application.home.HomeActivity">
<com.condecosoftware.core.dayview.DayView
android:id="@+id/dayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:timeColumnWidth="52dp"
app:timeColumnPadding="16dp"
app:hourHeight="96dp"
app:textSize="14sp"
app:eventNameTextSize="18sp"
app:hourSeparatorHeight="1dp"
app:timeTextColor="@color/slateGrey"
app:timeColumnBGColor="@color/white_2"
app:hourSeparatorColor="@color/white_2"
app:nowLineColor="@color/lipstick"
app:eventColumnBGColor="@android:color/white"
app:eventBGColor="@color/brightSkyBlue"
app:eventCornerRadius="3.8dp"
app:eventTextColor="@android:color/white"/>
</android.support.constraint.ConstraintLayout>
</layout>
现在,我应该注意,视图IS正在膨胀并正确显示(如果我删除onCreateView中回调的赋值)。
以下是导致NPE的代码(bobarly,Kotlin并不认为我尝试访问的变量可能为空(我不需要进行安全转换或任何事情来进行编译) ):
@SuppressLint("MissingSuperCall")
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val viewBinding = FragmentBookingsTodayBinding.inflate(inflater ?: LayoutInflater.from(context))
// Setup day view listeners
viewBinding.dayView.eventClickListener = this
viewBinding.dayView.eventLongPressListener = this
viewBinding.dayView.dayViewCallbacks = this
this.viewBinding = viewBinding
return viewBinding.root
}
我知道我正在对该方法进行超级调用,但正如您所看到的那样,我将返回底部的视图绑定根,这样就可以了。
有没有人有任何想法?
编辑(堆栈跟踪):
FATAL EXCEPTION: main
Process: com.condecosoftware.roombooking.develop, PID: 3949
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.condecosoftware.core.dayview.DayView.setEventClickListener(com.condecosoftware.core.dayview.DayView$EventClickListener)' on a null object reference
at com.condecosoftware.roombooking.application.home.FragmentBookingsToday.onCreateView(FragmentBookingsToday.kt:53)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:22002)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1028)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
at android.view.View.measure(View.java:22002)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
10-21 14:05:30.514 3949-3949/com.condecosoftware.roombooking.develop E/AndroidRuntime:
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)