3

我编写了一个自定义视图,用于在 24 小时垂直滚动时间轴中显示事件。

在扩充此视图的片段中的 onCreateView 方法中,我正在扩充视图,然后尝试设置视图公开的回调。但是,当我尝试在自定义视图上设置属性时(即自定义视图为空),我得到了一个空引用异常。

这是我的布局(约束布局内有我的自定义 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>

现在,我应该在这一点上注意到,视图正在膨胀并正确显示(如果我删除了 onCreateView 中回调的分配)。

这是导致 NPE 的代码(奇怪的是,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)
4

0 回答 0