我目前正在使用此代码尝试从一个活动中打开一个新活动,该活动是在3个活动之间刷卡的寻呼机适配器的一部分。
public class CustomPagerAdapter extends PagerAdapter implements OnClickListener {
private Context mContext;
public CustomPagerAdapter(Context context){
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup)
inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ModelObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
public void onClick(View view) {
//start new activity
Intent i = new Intent(mContext, SubjectActivity.class);
mContext.startActivity(i);
}
}
代码本身并没有抛出任何错误,但是当我运行应用程序时,它会在我点击按钮时停止。在我尝试实现onClick功能之前它没有遇到问题所以我确定问题在于我是如何编写的,但我无法弄清楚在哪里。这种开设新活动的方法有什么问题?