Button onClick doesn't work

10,220

Solution 1

Try these lines, i hope it works...

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

Solution 2

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));
        }
    });
}

Solution 3

> btn.setOnClickListener((OnClickListener) this);

This does not work because ViewPagerProjectActivity does not have the required

implements OnClickListener interface

Solution 4

try

 public class ViewPagerProjectActivity extends Activity implements OnClickListener

 then btn.setOnClickListener(this); 
Share:
10,220
yahya
Author by

yahya

Updated on August 20, 2022

Comments

  • yahya
    yahya over 1 year

    I made a viewPager with three layouts on every page... And i want to deal with a button on second page, but somehow i cannot... (P.S.: without button codes, my viewPager works) I just simply try to change background when clicked...

    Here's 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's 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)
    
  • yahya
    yahya about 12 years
    You're right, i just forget about it... But now it's done and compiler gives me a NullPointerException erron on this line "btn.setOnClickListener(this);" but why? :/
  • yahya
    yahya about 12 years
    Now compiler gives me a NullPointerException error on this line "btn.setOnClickListener( ... );"
  • yahya
    yahya about 12 years
    I'm getting still NullPointerException ... I can't figure out why? :/
  • Hiral Vadodaria
    Hiral Vadodaria about 12 years
    Have you tried whole code as i gave in my answer? dont use that onClicl() outside onCreate() as you do in your question.
  • yahya
    yahya about 12 years
    Yeap, i did... still gives me that.
  • Hiral Vadodaria
    Hiral Vadodaria about 12 years
    Strange!That shouldn't.using this code,you don't need to implement onClickListener.Remove it if you did that.But i think,you already got the answer so not a problem now!!
  • yahya
    yahya about 12 years
    Except "NullPointerException", yes i got my answer. Thanks.
  • yahya
    yahya about 12 years
    You want viewPager or the xml codes which contains the button ?
  • yahya
    yahya about 12 years
    I figure out why that is... Because i'm on main.xml but the button is on another xml file... But the problem is i'm using the viewPager, so it fill the context with the layouts, and i've got no idea about where should i write this button onClick???
  • Hiral Vadodaria
    Hiral Vadodaria about 12 years
    Please post your xml as well as your code.then i can help you on the same!
  • yahya
    yahya about 12 years
    I've change the position of the codes, and it worked. Problem's solved, thanks.