java - 在 Android Eclipse IDE 中使用 ImageButton

标签 java android eclipse button imageview

我正在尝试在 Eclipse IDE (ADT) 中使用 ImageButton

IDE 在编译时没有显示任何错误,但当我在模拟器中运行它时,它永远不会启动,而是显示“不幸的是,您的应用程序已停止工作”。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/ic_launcher" />

和main.activity.java

public class MainActivity extends Activity {


Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    public void addListenerOnButton() {

        final Context context = this;

        button1 = (Button) findViewById(R.id.imageView1);

        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, ImageActivity.class);
                startActivity(intent);   

            }

        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

最佳答案

 button1 = (Button) findViewById(R.id.imageView1);

Button更改为ImageView 并在 xml 中将此行添加到您的 ImageView

android:clickable="true"

关于java - 在 Android Eclipse IDE 中使用 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453957/

相关文章:

java - 有人可以帮助我使用 Android RemoteControlClient 吗?

java - 安卓 "main cannot be resolved or is not a field"

java - Java 中的日期到日期的转换

java - Intellij : Adding 'Interface' to my context menu in the Project Pane

java - 如何使 Java Web 应用程序完全无状态

java - 使用 jdbc 澄清 oracle 中的游标

java - 下面的代码中 asynctask 是否正确使用?

java - 如何将光标数据(DbHandler.java)添加到 ListView (MainActicity.java)?

java - Ant 脚本示例

Netbeans/Eclipse 无法识别 Java ME SDK