Android新建项目后 编译一些代码 运行程序会闪退 不编译代码不会 求大神帮助啊 下有图

图片说明图片说明
图片说明
Button but = (Button) findViewById(R.id.imageButton12);
final ImageView images1 = (ImageView) findViewById(R.id.ImageView1);
删去这两行后 有可以运行
图片说明图片说明图片说明![图片说明](https://img-ask.csdn.net/upload/201705/21/1495370701_142669.png)图片说明图片说明

闪退,报的什么错误?贴上来看看

Button but = (Button) findViewById(R.id.imageButton12);
final ImageView images1 = (ImageView) findViewById(R.id.ImageView1);
把这两行代码放到onCreate方法里面,setContentView后面。
因为你的界面布局还没set进去,findViewById取到的肯定取不到

 Button but = (Button) findViewById(R.id.imageButton12);
final ImageView images1 = (ImageView) findViewById(R.id.ImageView1);

这两行应该放到 setContentView 之后。

setContentView作用是提供布局继承关系的 根节点

不先运行setContentView,找不到下面的布局控件。

控件初始化要在OnCreate方法里

1L正解,所有的初始化都应该放在OncCeate 这个函数里,因为他要先启动你的布局文件,再去寻找你的控件完成初始化操作。

findViewById 放在 onCreate方法里面,,在外面声明一下就行

两个不同的错误,1.如1l所那两行代码放到onCreate方法里面,setContentView后面 2.你那个R.id.imageButton12在布局文件中应该是ImageButton吧,不能强转成Button

1.把你的初始化控件的那两行代码放到onCreate里面的setContentView后面
2.你的控件一个是TextView一个是ImageButton,你用ImageView和Button接收,能不报错才怪,把你那两行代码换成
ImageButton but = (ImageButton) findViewById(R.id.imageButton12);
final TextView images1 = (TextView) findViewById(R.id.ImageView1);