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) ;

更多相关文章

  1. 单击按钮后更改Frame的内容窗格
  2. 【JAVA】用java编写程序时总提示缺少方法主体或声明抽象的原因
  3. 在Java中选择showMessageDialog框右上角的x按钮退出系统?
  4. 关于Javascript中声明变量、函数的笔记

随机推荐

  1. 关于PHP循环读取json数据的方法
  2. 详解PHP面向对象之后期静态绑定
  3. 速看!php artisan 命令列表
  4. 或许你想去掉Response Headers中的X-Powe
  5. 关于PHP CURL上传二进制流图片
  6. 关于php preg_replace_callback回调函数
  7. PHP读取Excel图片对象,并保存替换为相对路
  8. PHP最大连接数的设置及php-fpm高并发参数
  9. PHP基础案例四:展示学生列表
  10. PHP实用函数集合