Android 什么';更改图像按钮背景的代码有什么问题?

Android 什么';更改图像按钮背景的代码有什么问题?,android,imagebutton,Android,Imagebutton,我正在尝试使用以下代码更改imagebutton的图像。Eclipse崩溃,但没有发现错误…我也使用了setBackgroundResource而不是setImageResource,但它不起作用。日志中没有任何内容,错误中没有任何内容。请帮我找到正确的路 我有两张这个imagebutton的图片编辑打开和编辑关闭 xml 爪哇 我的xml看起来像 <ImageButton android:id="@+id/buttonblock" android:layout_width="

我正在尝试使用以下代码更改imagebutton的图像。Eclipse崩溃,但没有发现错误…我也使用了setBackgroundResource而不是setImageResource,但它不起作用。日志中没有任何内容,错误中没有任何内容。请帮我找到正确的路

我有两张这个imagebutton的图片编辑打开和编辑关闭 xml

爪哇

我的xml看起来像

<ImageButton android:id="@+id/buttonblock" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="0sp" 
    android:scaleType="centerCrop" 
    android:background="@drawable/edit_off" /> 

在xml中
使用src而不是后台
和
ImageButton btn=(ImageButton)findViewById(R.id.buttonblock);
btn.setImageResource(R.drawable.newimage);
试试这个

imageblockButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_on));

我的xml看起来像…非常感谢…但是,它还没有工作。我正在获取[2013-12-08 18:39:41-IncomingCallNO]新的emulator发现:emulator-5554[2013-12-08 18:39:41-IncomingCallNO]正在等待HOME('android.process.acore')启动…非常感谢。我尝试了你的建议,但没有成功…[2013-12-08 18:39:41-IncomingCallNO]找到了新的emulator:emulator-5554[2013-12-08 18:39:41-IncomingCallNO]正在等待HOME('android.process.acore')启动……我得到了同样的结果。我认为编译器不起作用了…现在重新启动eclipse和emulatorRestarting,除非我还有另一个问题。如上所示,按下imagebutton,屏幕将移动到其他活动。然而,当我回到这个屏幕时,它显示的是“新图像”,而不是旧图像。单击时,新图像应显示。你能再帮我一次吗?
In your xml    
<ImageButton android:id="@+id/buttonblock" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="0sp" 
        android:scaleType="centerCrop" 
        android:src="@drawable/edit_off" />  

    use src instead of background
    and 
    ImageButton btn = (ImageButton)findViewById(R.id.buttonblock);
    btn.setImageResource(R.drawable.newimage);
imageblockButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_on));