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);
Author by
yahya
Updated on August 20, 2022Comments
-
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 about 12 yearsYou'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 about 12 yearsNow compiler gives me a NullPointerException error on this line "btn.setOnClickListener( ... );"
-
yahya about 12 yearsI'm getting still NullPointerException ... I can't figure out why? :/
-
Hiral Vadodaria about 12 yearsHave you tried whole code as i gave in my answer? dont use that onClicl() outside onCreate() as you do in your question.
-
yahya about 12 yearsYeap, i did... still gives me that.
-
Hiral Vadodaria about 12 yearsStrange!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 about 12 yearsExcept "NullPointerException", yes i got my answer. Thanks.
-
yahya about 12 yearsYou want viewPager or the xml codes which contains the button ?
-
yahya about 12 yearsI 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 about 12 yearsPlease post your xml as well as your code.then i can help you on the same!
-
yahya about 12 yearsI've change the position of the codes, and it worked. Problem's solved, thanks.