Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button onClick doesn't work

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)
like image 582
yahya Avatar asked Dec 10 '22 03:12

yahya


2 Answers

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);
like image 104
meltemyldrim Avatar answered Dec 21 '22 11:12

meltemyldrim


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));
        }
    });
}
like image 37
Hiral Vadodaria Avatar answered Dec 21 '22 09:12

Hiral Vadodaria