java.lang.IllegalStateException: Could not execute method of the activity
2015-04-22 17:16
501 查看
转载:http://www.xuebuyuan.com/2097467.html
Android编程里面很多问题其实都不算大问题,很多错误也是小错误,比如这次要说的
LogCat信息:
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
说白了就是无法执行和activity相关的方法,为什么无法执行呢,因为你给系统的是空引用,出现空指针错误
造成这种因素的原因很多,我目前已知的,就是代码的顺序问题,在onCreate方法中,应有如下代码
这是一般的写法,如果照这样写,不会出现任何问题
注意现在把
的顺序换一下
为什么呢,因为绑定控件必须在整个界面出来之后,也就是说,系统还不知道使用的是哪个xml文件的时候,你告诉系统把xml文件中id为username的赋给username,系统当然会赋null,你对null执行getText()当然会出错.
所以写代码的时候注意先后顺序,setContentView先执行之后再进行相关控件的绑定.
自己错的地方:
自己的activity没有加进去,找不到,报错
Android编程里面很多问题其实都不算大问题,很多错误也是小错误,比如这次要说的
LogCat信息:
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
说白了就是无法执行和activity相关的方法,为什么无法执行呢,因为你给系统的是空引用,出现空指针错误
造成这种因素的原因很多,我目前已知的,就是代码的顺序问题,在onCreate方法中,应有如下代码
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username = (EditText) findViewById(R.id.username); pwd = (EditText) findViewById(R.id.pwd); }
这是一般的写法,如果照这样写,不会出现任何问题
注意现在把
setContentView(R.layout.activity_main);
的顺序换一下
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); username = (EditText) findViewById(R.id.username); pwd = (EditText) findViewById(R.id.pwd); setContentView(R.layout.activity_login); }这样只要执行和activity相关的方法,就会出现Could not execute method of the activity
为什么呢,因为绑定控件必须在整个界面出来之后,也就是说,系统还不知道使用的是哪个xml文件的时候,你告诉系统把xml文件中id为username的赋给username,系统当然会赋null,你对null执行getText()当然会出错.
所以写代码的时候注意先后顺序,setContentView先执行之后再进行相关控件的绑定.
自己错的地方:
自己的activity没有加进去,找不到,报错
相关文章推荐
- java.lang.IllegalStateException: Could not execute method of the activity
- java.lang.IllegalStateException: Could not find a method capture(View) in the activity class
- Android 学习之异常总结--java.lang.IllegalStateException:Could not execute method of the activity
- java.lang.IllegalStateException: Could not execute method of the activity
- java.lang.IllegalStateException: Could not find a method onStartSingleInstanceActivityClickLisenter(
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException:Make sure the content of your adapter is not modified from a backgro
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receiv
- 新增activity报错,Could not execute method of the activity
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- Could not execute method of the activity
- Could not execute method of the activity Android
- java.lang.IllegalStateException:The content of the adapter has changed but ListView did not receiv问题
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- java.lang.IllegalStateException: Could not find a method false(View) in the activity class com.xuan
- hbase java.lang.IllegalArgumentException: Could not resolve the DNS name of X.X.X.X:60020
- 解决Could not execute method of the activity
- java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the A