如何同时使用Kotlin Android扩展和数据绑定?

时间:2018-07-01 14:07:28

标签: android kotlin

我想用Kotlin创建一个Android项目,所以我用kotlin和Databinding构建它。没错。但是,当我使用Kotlin Android扩展程序或Butterknife时,无法正常使用从该项目构建的应用程序。

这是我的片段类

class MainFragment: Fragment() {

private var stock: StockQuote? = null

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
    val binding: FragmentMainBinding = DataBindingUtil.inflate(inflater!!, R.layout.fragment_main, container, false)
    binding.stock = stock
    search_et.setOnFocusChangeListener { v, hasFocus ->
        if(!hasFocus){
            val newStock = ScrapingUtil.getQuote(search_et?.text.toString())
            if(newStock != null){
                stock?.name = newStock.name
            }
        }
    }
    return binding.root
}

}

我最初尝试使用Butterknife,但没有成功,我要绑定的View为null。然后我尝试使用Kotlin Android扩展,它在运行时引发了错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: cn.mister.stockcalculator, PID: 30010
              java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.mister.stockcalculator/cn.mister.stockcalculator.MainActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2736)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1604)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:191)
                  at android.app.ActivityThread.main(ActivityThread.java:6016)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821)
               Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at cn.mister.stockcalculator.MainActivity.onCreate(MainActivity.kt:10)
                  at android.app.Activity.performCreate(Activity.java:6370)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1604) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:191) 
                  at android.app.ActivityThread.main(ActivityThread.java:6016) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821) 
               Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                  at cn.mister.stockcalculator.MainActivity.onCreate(MainActivity.kt:10) 
                  at android.app.Activity.performCreate(Activity.java:6370) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2807) 
                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1604) 
                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                  at android.os.Looper.loop(Looper.java:191) 
                  at android.app.ActivityThread.main(ActivityThread.java:6016) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:821) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnFocusChangeListener(android.view.View$OnFocusChangeListener)' on a null object reference
                  at cn.mister.stockcalculator.MainFragment.onCreateView(MainFragment.kt:23)
                  at android.app.Fragment.performCreateView(Fragment.java:2236)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1139)
                  at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1241)
                  at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2236)
                  at android.app.FragmentController.onCreateView(FragmentController.java:98)
                  at android.app.Activity.onCreateView(Activity.java:5678)
                  at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
                  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我在Kotlin Android Extensions and Fragments找到了答案。

数据绑定无关紧要,您应该使用search_et方法直接访问onViewCreated