升级到Studio 4.2 Canary 5后,Android Studio出现数据绑定错误

时间:2020-07-22 08:14:53

标签: android android-studio

我正在使用数据绑定来增加片段的视图。在将Android Studio升级到4.2版Canary 5之前,它工作正常。它还要求我将gradle版本升级到4.2.0-alpha05,从而将默认语言功能升级到Java 8。但是现在数据绑定部分抛出了NullPointerException。我在这里需要帮助。下面是错误的踪迹。

2020-07-22 01:11:13.437 18307-18307/com.deamat.mydeamat.debug E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.deamat.mydeamat.debug, PID: 18307
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deamat.mydeamat.debug/com.deamat.mydeamat.MainActivity}: java.lang.IllegalStateException: FragmentSignupBinding.in…          false
            ) must not be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
     Caused by: java.lang.IllegalStateException: FragmentSignupBinding.in…          false
            ) must not be null
        at com.deamat.auth.SignupFragment.onCreateView(SignupFragment.kt:27)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
        at android.app.Activity.performStart(Activity.java:6959)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2890)
            ... 9 more

这是数据绑定调用

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
       val binding = FragmentSignupBinding.inflate(
            inflater,
            container,
            false
        )

        ...
}

注意:我在应用程序中使用了多个模块,并且已在每个模块中启用了数据绑定。但是,login_fragment布局与Fragment类位于同一模块内。

2 个答案:

答案 0 :(得分:2)

自从更新到AS 4.2 Canary 5 / Gradle 4.2.0-alpha05以来,数据绑定也对我们不利。我们甚至无法构建我们的应用。

在我们的情况下,它似乎与自定义BindingAdapter有关。从布局文件中删除它们的用法后,就可以构建该应用程序-尽管缺少相应的逻辑。确实不是解决方案,所以我们现在不得不降级到Canary 4。

答案 1 :(得分:-1)

这样做:

class PracticeFragment:Fragment() {
    private lateinit var binding: FragmentPracticeBinding//declare your binding 

    companion object {
        fun newInstance() = PracticeFragment()
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPracticeBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    }
}