【发布时间】:2013-05-04 20:33:58
【问题描述】:
我对 Android 开发相当陌生,我正在尝试创建一个带有按钮的滑动轮播,这些按钮可以链接到其他活动,这就是我目前所拥有的......
Main.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//Sliding Carousel controls
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
}
//Sliding Carousel controls
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.carousel_1;
break;
case 1:
resId = R.layout.carousel_2;
break;
case 2:
resId = R.layout.carousel_3;
Button myButton = (Button) findViewById(R.id.paymybill);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
break;
case 3:
resId = R.layout.carousel_4;
break;
case 4:
resId = R.layout.carousel_5;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
我目前正在尝试让按钮在案例 2 上工作(但最终所有案例都有 2 到 4 个按钮)。
当我用
将它加载到我的模拟器上时 Button myButton = (Button) findViewById(R.id.paymybill);
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
break;
注释掉它可以完美运行,但是当我包含它时它会在加载时崩溃。
我认为问题在于我没有正确地将按钮指向寻呼机视图并且应用程序崩溃了,因为 main_activity 没有我引用的任何按钮或布局。
过去 2 天我一直在研究类似的问题,但我不确定我做错了什么,例如 this case
v = inflater.inflate(R.layout.dashboard_media, null);
接缝是同样的问题,但它使用了不同的适配器实现,我不确定我可以替换什么以及我不能替换什么(我已经尝试过并且没有任何接缝可以工作),正如我所说的那样对此很陌生,我相信这是我想念的简单东西!
谁能帮忙?
【问题讨论】:
标签: android android-layout android-viewpager