私は2つの活動の間にページスライダーを作ろうとしているプロジェクトに固執しました。
- 各アクティビティが独立して機能することはわかっていますが、スライドパッティング用にPageAdapterを設定しようとすると、次のようになります。
また、マニフェストでアクティビティを記述し、JavaでXMLからすべての変数を初期化しますが、ボタンにonClickListenerを設定した瞬間に問題が発生します。
LayoutInflater inflater = LayoutInflater.from(this); リストページ=newArrayList();
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){
}
} 2番目のアクティビティ:
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);
}
}