本文主要是介绍《Error IllegalStateException异常》,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、开发背景
使用butterknife:8.4.0 框架,用插件实现实例化控件。
二、异常日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.bql.vehiclemounteds/cn.bql.vehiclemounted.vehiclemounteds.activity.CarInspectionActivity}: java.lang.IllegalStateException: Required view 'content' with ID 2131689669 for field 'content' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3168)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278)at android.app.ActivityThread.access$1000(ActivityThread.java:211)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:6918)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)Caused by: java.lang.IllegalStateException: Required view 'content' with ID 2131689669 for field 'content' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.at butterknife.internal.Utils.findRequiredView(Utils.java:138)at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:150)at cn.bql.vehiclemounted.vehiclemounteds.activity.CarInspectionActivity_ViewBinding.<init>(CarInspectionActivity_ViewBinding.java:30)at java.lang.reflect.Constructor.newInstance(Native Method)at java.lang.reflect.Constructor.newInstance(Constructor.java:288)at butterknife.ButterKnife.createBinding(ButterKnife.java:199)at butterknife.ButterKnife.bind(ButterKnife.java:124)at cn.bql.vehiclemounted.vehiclemounteds.activity.BaseActivity.onCreate(BaseActivity.java:32)at android.app.Activity.performCreate(Activity.java:6609)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278)?at android.app.ActivityThread.access$1000(ActivityThread.java:211)?at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1705)?at android.os.Handler.dispatchMessage(Handler.java:102)?at android.os.Looper.loop(Looper.java:145)?at android.app.ActivityThread.main(ActivityThread.java:6918)?at java.lang.reflect.Method.invoke(Native Method)?at java.lang.reflect.Method.invoke(Method.java:372)?at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)?at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)?java.lang.IllegalStateException: Required view 'content' with ID 2131689669 for field 'content' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.at butterknife.internal.Utils.findRequiredView(Utils.java:138)at butterknife.internal.Utils.findRequiredViewAsType(Utils.java:150)at cn.bql.vehiclemounted.vehiclemounteds.activity.CarInspectionActivity_ViewBinding.<init>(CarInspectionActivity_ViewBinding.java:30)at java.lang.reflect.Constructor.newInstance(Native Method)at java.lang.reflect.Constructor.newInstance(Constructor.java:288)at butterknife.ButterKnife.createBinding(ButterKnife.java:199)at butterknife.ButterKnife.bind(ButterKnife.java:124)at cn.bql.vehiclemounted.vehiclemounteds.activity.BaseActivity.onCreate(BaseActivity.java:32)at android.app.Activity.performCreate(Activity.java:6609)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3278)at android.app.ActivityThread.access$1000(ActivityThread.java:211)at an
三、异常原因
Activity里面加载了一个控件,但是XML里并没有这个控件。使用butterknife这个框架的时候,有时候会出现在这个Activity里面加载到另外一个布局的控件,然后你会莫名其妙的发现报错了,但是找到位置,对应的控件,戳进去之后是有xml对象的。再仔细看看,原来是Activity和对应的xml不一样。
四、异常处理
找到相应的Activity,然后把多余的控件实例化代码删除即可。
如上异常中CarInspectionActivity.class添加的content控件,CarInspectionActivity.class对应的XML布局里面并没有对应的控件,所以应该删除。
这篇关于《Error IllegalStateException异常》的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!