我坚持试图在两个活动之间制作Page Slider的项目。
此外,我在Manifest中编写活动,在Java中初始化XML中的所有变量,但问题始于我为我的按钮设置onClickListener的那一刻。
LayoutInflater inflater = LayoutInflater.from(this); List pages = new ArrayList();
View page = inflater.inflate(R.layout.photo, null);
pages.add(page);
page = inflater.inflate(R.layout.cameragrid, null);
pages.add(page);
CameraPagerAdapter pagerAdapter = new CameraPagerAdapter(pages);
ViewPager viewPager = new ViewPager(this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0); //view being shown firstly
setContentView(viewPager);
而不是*setContentView(R.layout.photo);*
它不会启动。
我错过了什么?谢谢你提前。
第一项活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
makeAdditionalPageSlide(); //>>> problems started here
initialize();
InputStream is = getResources().openRawResource(R.drawable.dinam);
bmp = BitmapFactory.decodeStream(is);
}
public void makeAdditionalPageSlide(){
LayoutInflater inflater = LayoutInflater.from(this);
List<View> pages = new ArrayList<View>();
View page = inflater.inflate(R.layout.photo, null);
pages.add(page);
page = inflater.inflate(R.layout.cameragrid, null);
pages.add(page);
CameraPagerAdapter pagerAdapter = new CameraPagerAdapter(pages);
ViewPager viewPager = new ViewPager(this);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0); //view being shown firstly
setContentView(viewPager);
}
private void initialize() {
// TODO Auto-generated method stub
iv = (ImageView) findViewById (R.id.ivReturnedPic);
ib = (ImageButton) findViewById (R.id.ibTakePic);
b = (Button) findViewById(R.id.bSetWall);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
PageAdapter:
public class CameraPagerAdapter extends PagerAdapter {
List<View> pages = null;
public CameraPagerAdapter(List<View> pages){
this.pages = pages;
}
@Override
public Object instantiateItem(View collection, int position){
View v = pages.get(position);
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View collection, int position, Object view){
((ViewPager) collection).removeView((View) view);
}
@Override
public int getCount(){
return pages.size();
}
@Override
public boolean isViewFromObject(View view, Object object){
return view.equals(object);
}
@Override
public void finishUpdate(View arg0){
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1){
}
@Override
public Parcelable saveState(){
return null;
}
@Override
public void startUpdate(View arg0){
}
} 第二个活动:
public class CameraGrid extends Activity implements OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cameragrid);
GridView gv = (GridView) findViewById(R.id.cameragrid);
CameraImageAdapter ia = new CameraImageAdapter(getApplicationContext());
gv.setAdapter(ia);
//gridview.setOnItemClickListener(this);
gv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> arg0, View iv, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(),CameraImageDetails.class);
i.putExtra("id", position);
startActivity(i);
}
}
答案 0 :(得分:0)
我的建议是使用ViewPager和setContentView(R.layout.mainActivity)的占位符制作mainActivity.xml布局
与PageViewAdapter中的页面视图内容相关的内容。
也许我不太明白你试图做的逻辑,但它似乎有点令人困惑......:S