【问题标题】:Using ImageButton in Android Eclipse IDE在 Android Eclipse IDE 中使用 ImageButton
【发布时间】:2014-03-17 12:12:00
【问题描述】:

我正在尝试在 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;
    }

}

【问题讨论】:

  • 请发布您的 logcat 输出
  • 发布您的布局 xml 文件

标签: java android eclipse button imageview


【解决方案1】:
 button1 = (Button) findViewById(R.id.imageView1);

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

android:clickable="true"

【讨论】:

    【解决方案2】:

    那是因为您将 ImageView 转换为 Button,这是不正确的。

    您的变量button1 必须是ImageView 类型。

    您也可以使用ImageButton 类型,但请确保在 XML 布局和 Java 代码中使用兼容的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2014-01-13
      相关资源
      最近更新 更多