OnClick button not working

I made a viewPager with three layouts on each page ... And I want to deal with a button on the second page, but somehow I can’t ... (PS: without button codes, my viewPager works) I will just try to change the background when you press...

Here are my codes:

public class ViewPagerProjectActivity extends Activity implements OnClickListener{ Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPagerAdapter adapter = new ViewPagerAdapter( this ); ViewPager pager = (ViewPager)findViewById( R.id.viewpager ); pager.setAdapter( adapter ); pager.setCurrentItem(0); } public void OnClick(View v) { if(v.equals(btn)) { LinearLayout l = (LinearLayout) findViewById(R.id.deneme2); l.setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); } } } 

And here is the error:

 E/AndroidRuntime(273): FATAL EXCEPTION: main E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yahya.ViewPagerProject/com.yahya.ViewPagerProject.ViewPagerProjectActivity}: java.lang.ClassCastException: com.yahya.ViewPagerProject.ViewPagerProjectActivity E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
+4
source share
7 answers

Try these lines, I hope this works ...

 View deneme2lout = (View) getView.inflate(R.layout.deneme2,null); btn = (Button) deneme2lout.findViewById(R.id.button1); btn.setOnClickListener(this); 
+10
source

Try using:

 public class ViewPagerProjectActivity extends Activity { Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPagerAdapter adapter = new ViewPagerAdapter( this ); ViewPager pager = (ViewPager)findViewById( R.id.viewpager ); pager.setAdapter( adapter ); pager.setCurrentItem(0); LinearLayout l = (LinearLayout) findViewById(R.id.layout1); btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { l.setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); } }); } 
+2
source
 > btn.setOnClickListener((OnClickListener) this); 

This does not work because ViewPagerProjectActivity does not have the required

implements OnClickListener interface

+1
source

try

  public class ViewPagerProjectActivity extends Activity implements OnClickListener then btn.setOnClickListener(this); 
+1
source

Even I encountered such a problem when I add a user interface UI element to an XML file.

I simply deleted this particular element from the Xml file and built the project and returned the file again. The problem is resolved. It looks crazy ... But try once.

0
source

When you try to handle a button click, you have two options

  • In the layout xml file, set android: onClick to something like "myButtonClick". Then, in your linked Activity.java file where you are going to use this layout, there is a public void myButtonClick(View v) method. Then enter your logic.
  • In your onCreate () put

     btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //Your logic here } }); 
0
source

I think this will help you.

use buttons in android ...

display the button in the XML file using the button in the java class file as ...

 Button bt=(Button)findViewById(R.layout.button_id_in_your_xml); 

next .. call onclickListener for the bt button as follows.

 bt.setOnClickListener(new View.onClickListener(){ public void onClick(View arg0) { //write what you want to do when you click the button } ); 
0
source

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


All Articles