不幸的是,在声明按钮时,模拟器中出现了错误
16lz
2021-01-22
When i declare a button in MainActivity.java , i get Unfortunately error in emulator
当我在MainActivity中声明一个按钮时。java,很不幸,我在模拟器中出错了
But when i comment the button declaration , no errors
但是当我注释按钮声明时,没有错误
This is MainActivity.java code :
这是MainActivity。java代码:
package com.MalekAlrwily.multiplication;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btn = (Button)findViewById(R.id.btn_start) ;
}
}
And this is main.xml code :
这是主要的。xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main2" >
<ImageButton
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:contentDescription="@android:string/ok"
android:src="@drawable/sun" />
</RelativeLayout>
Please help
请帮助
Thanks in advance .
提前谢谢。
4 个解决方案
#1
5
It's because, you declare in xml
as ImageButton
and dynamically, you declare Button
.
这是因为,您在xml中声明为ImageButton,并动态地声明按钮。
It should be:
应该是:
final ImageButton btn = (ImageButton) findViewById(R.id.btn_start) ;
更多相关文章
- 单击按钮后更改Frame的内容窗格
- 【JAVA】用java编写程序时总提示缺少方法主体或声明抽象的原因
- 在Java中选择showMessageDialog框右上角的x按钮退出系统?
- 关于Javascript中声明变量、函数的笔记