解决Could not execute method of the activity
来源:互联网 发布:结构强度分析软件 编辑:程序博客网 时间:2024/05/04 14:51
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方法中,应有如下代码
@Overrideprotected 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);
的顺序换一下
@Overrideprotected 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先执行之后再进行相关控件的绑定.
0 0
- 解决Could not execute method of the activity
- 解决Could not execute method of the activity
- Could not execute method of the activity
- 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 execute method of the activity
- 新增activity报错,Could not execute method of the activity
- 解决Failed to execute request because the App-Domain could not be created问题
- java.lang.IllegalStateException: Could not find a method capture(View) in the activity class
- Could not find a method XXX(View) in the activity class
- java.lang.IllegalStateException: Could not find a method false(View) in the activity class com.xuan
- Invocation of init method failed; Could not get a resource from the pool
- method to solve the problem : Could not cast value of type 'NSManagedObject_ to
- 【bug】Could not find method compile() 解决
- SQLGrammarException :could not execute query a.user not exists解决
- apt-get The method driver /usr/lib/apt/methods/http could not be found错误解决
- could not execute query
- Could not execute PROPPATCH
- php中stripslashes和addslashes的区别
- warning LNK4070
- boost::algorithm学习
- Speex手册(六)——语音处理API(2)与格式和标准
- FreeBSD安全配置小结
- 解决Could not execute method of the activity
- 有类路由协议与无类路由协议区别
- String str=new String("123"); 究竟创建了几个对象
- DUILIB界面库出现控件问题的解决
- SQL SERVER 2008 彻底删除
- 毕向东的java视频的Day15.
- ubuntu12 php mysql问题
- 图片数据Base64编解码(java实现)
- 编程学习方法总结——仅此献给自己