解决Could not execute method of the activity
2014-02-06 22:18
369 查看
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方法中,应有如下代码
这是一般的写法,如果照这样写,不会出现任何问题
注意现在把
的顺序换一下
这样只要执行和activity相关的方法,就会出现Could not execute method of the activity
为什么呢,因为绑定控件必须在整个界面出来之后,也就是说,系统还不知道使用的是哪个xml文件的时候,你告诉系统把xml文件中id为username的赋给username,系统当然会赋null,你对null执行getText()当然会出错.
所以写代码的时候注意先后顺序,setContentView先执行之后再进行相关控件的绑定.
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先执行之后再进行相关控件的绑定.
相关文章推荐
- java.lang.IllegalStateException: Could not execute method of the activity
- java.lang.IllegalStateException: Could not execute method of the activity
- Could not execute method of the activity Android
- Could not execute method of the activity
- Android 学习之异常总结--java.lang.IllegalStateException:Could not execute method of the activity
- 新增activity报错,Could not execute method of the activity
- java.lang.IllegalStateException: Could not execute method of the activity
- 解决“could not find gem 'turn <>=0 ' in any of the gem sources listed in your gemfile”错误
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决
- Could not find the required version of the Java(TM)2 Runtime Environment in '(null)' 解决办法
- some of the properties associated with the solution could not be read解决方法
- The version of CKEditor could not be detected.解决方法
- java.lang.IllegalStateException: Could not find a method false(View) in the activity class com.xuan
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决
- java.lang.IllegalStateException: Could not find a method capture(View) in the activity class
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决办法。
- some of the properties associated with the solution could not be read解决方法
- Server Application Unavailable 【Failed to execute request because the App-Domain could not be created.】的解决办法
- This platform could not be created because a solution platform of the same name already exists解决方法