イメージボタンがあるところに簡単なアプリケーションを作成しました。イメージボタンイベントが動作しない
画像ボタンをクリックしてトーストをしたいです。
私の問題は、その画像ボタンを見ることができますが、その画像ボタンをクリックしても何も起こりません。私はそのコードで問題を見つけることができません。 ここに私のコードです。ここで
は私のXMLファイルが
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:src="@drawable/myimage"/>
</RelativeLayout>
あるContentMain.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class ContentMain extends AppCompatActivity {
ImageButton imgButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
imgButton =(ImageButton)findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
}
});
}
}
私はまた、あなたのコード内のすべてのエラーを見つけることができません。 'Toast.makeText'行に' getApplicationContext() 'の代わりに' ContentMain.this'を書いて、違いがあるかどうかを調べることをお勧めします。あなたは何の誤りもありませんよね?また、 'onClick'リスナーで' log'を試みて、その関数に入るかどうかを調べます: 'Log.i(" YOOOO "、" I'm here ");' – Vucko
ブレークポイントを入れてデバッグするあなたのコード。 – meh