gpt4 book ai didi

android - 按钮 onClick 不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:49 25 4
gpt4 key购买 nike

我制作了一个 viewPager,每页都有三种布局......我想处理第二页上的按钮,但不知何故我不能......(PS:没有按钮代码,我的 viewPager 工作)我只是简单地尝试单击时更改背景...

这是我的代码:

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

这是错误:

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)

最佳答案

尝试这些行,我希望它有效...

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

关于android - 按钮 onClick 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9096298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com