我创建一个viewPager,在每一个页面上有三个布局。在第二个页面上我想处理按钮事件,当点击按钮时背景颜色会变。但是现在我的代码不能运行。去掉button代码,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)