android ButterKnife与xml常见问题的解决办法

admin 2019-09-25 AM 1322℃ 0条

1、Required view 'tab_1' with ID 2131297054 for field 'tab1' was not found.的解决办法

报错信息:

Process: com.abc.dddd, PID: 13251
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.dddd/com.gemdale.decorate.base.base.MaintenanceDetailActivity}: java.lang.IllegalStateException: Required view 'tab_1' with ID 2131297054 for field 'tab1' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2487)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
        at android.app.ActivityThread.access$1100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5603)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
     Caused by: java.lang.IllegalStateException: Required view 'tab_1' with ID 2131297054 for field 'tab1' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
        at butterknife.internal.Utils.findRequiredView(Utils.java:88)
        at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:100)
        at com.gemdale.decorate.base.base.MaintenanceDetailActivity_ViewBinding.<init>(MaintenanceDetailActivity_ViewBinding.java:39)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at butterknife.ButterKnife.bind(ButterKnife.java:170)
        at butterknife.ButterKnife.bind(ButterKnife.java:99)
        at com.gemdale.uilibrary.base.BaseUIActivity.onCreate(BaseUIActivity.java:72)
        at com.gemdale.decorate.base.base.base.BaseActivity.onCreate(BaseActivity.java:35)
        at com.gemdale.decorate.base.base.MaintenanceDetailActivity.onCreate(MaintenanceDetailActivity.java:73)
        at android.app.Activity.performCreate(Activity.java:6355)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5603) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 

该工程使用ButterKnife进行View注入,因为之前已经排查过别的问题,比如版本匹配和Gradle匹配等,如果不存在环境有问题。
网上有人说是Id的冲突,但是Id冲突应该局限于页面内才对,搜索了一次也无果,但还是抱着试一试的想法去更换了,从Tab更换到Tab1问题依然没有解决,报的错一模一样。

回到问题层面上分析,假设代码什么的都是OK的,只是由于疏忽缺了某个步骤:

1、not found 说明ButterKnife 在layout里面没有找到,但是layout.xml里面明明有。
2、layout.xml 里面有,但是没找到,说明layout没有加载到内存中或者是ButterKnife.bind 的时候layout没有加载到内存中。

带着这样的疑问审查代码,果然`在Base类别里面的默认加载的layout还没完成,但是子类的ButterKnife.bind(this);已经提前去注入View。

所以造成了View not found的情况。

解决办法:确保代码setContentView 在 ButterKnife.bind(this)之前即可,注意不要也基类的加载顺序。

2、View 'scoller_container' with ID 2131296990 for field 'scollerContainer' was of the wrong type.的解决办法

报错信息:

Caused by: java.lang.IllegalStateException: View 'scoller_container' with ID 2131296990 for field 'scollerContainer' was of the wrong type. See cause for more info.
        at butterknife.internal.Utils.castView(Utils.java:109)
        at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:101)
        at com.gemdale.decorate.base.base.MaintenanceDetailActivity_ViewBinding.&lt;init&gt;(MaintenanceDetailActivity_ViewBinding.java:39)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at butterknife.ButterKnife.bind(ButterKnife.java:170)
        at butterknife.ButterKnife.bind(ButterKnife.java:99)
        at com.gemdale.uilibrary.base.BaseUIActivity.onCreate(BaseUIActivity.java:72)
        at com.gemdale.decorate.base.base.base.BaseActivity.onCreate(BaseActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:6355)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5603) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
     Caused by: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.LinearLayout

解决问题:

关键词:the wrong type, android.widget.ScrollView cannot be cast to android.widget.LinearLayout;
解决方案:将 scoller_container 对应的类型改为 android.widget.ScrollView即可。

标签: ButterKnife

非特殊说明,本博所有文章均为博主原创。

评论啦~