Android test app not working

I am new to android and trying to create a small application.

my java file:

//creating object for imagebutton class ImageButton mainButton = (ImageButton) findViewById(R.id.imageButtonone); //creating onclick listener mainButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //show message Toast toast = Toast.makeText(SkeletonActivity.this, "Button Pressed", Toast.LENGTH_LONG); toast.show(); } }); 

XML:

 <ImageButton android:id="@+id/imageButtonone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/dog8" /> 

where imageButton id = imageButtonone.

I can not type "Button pressed." Please, help!

+4
source share
4 answers

1) maybe your code will not reach the onClick method. check if your main element is empty or not.

2) in the eclipse menu: project-> clean (your project)

+1
source
 public class Abccls extends Activity implements View.OnClickListener { /** Called when the activity is first created. */ //@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.Button01); btn.setOnClickListener(this); } public void onClick(View view) { if(view == btn) Toast.makeText(this, "It is a toast ", Toast.LENGTH_SHORT).show(); } } 
+1
source

in setOnClickListener do View.OnClickListener . I think this will solve the problem.

  //creating object for imagebutton class ImageButton mainButton = (ImageButton) findViewById(R.id.imageButtonone); //creating onclick listener mainButton.setOnClickListener(new **View.OnClickListener()** { @Override public void onClick(View v) { //show message Toast toast = Toast.makeText(SkeletonActivity.this, "Button Pressed", Toast.LENGTH_LONG).show(); } 

});

+1
source

check if your image button is empty or not? Did you get any exception?

0
source

Source: https://habr.com/ru/post/1383375/


All Articles