如何在viewpager中为每个视图启动活动?

时间:2016-07-02 13:26:36

标签: android android-fragments android-viewpager

您好我ViewPager中有Fragment,其中显示了一些图像。我想在点击图片时为每个Activity开始一个新的View。我的意思是我在ViewPager使用circleadapter显示一些图片,我想用缩放选项为每个图像打开一个新的寻呼机。

这是我的adapter.java

public class Adapter extends PagerAdapter {

    private Activity _activity;

    Context context;
    LayoutInflater layoutinflater;

    public Adapter(Context context) {
        context = context;
       layoutinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    int[] mResources = {
            R.drawable.img1,
            R.drawable.img2,

    };

    @Override
    public int getCount() {

        return mResources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View itemView = layoutinflater.inflate(R.page_1, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.resim);
        imageView.setImageResource(mResources[position]);
        container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }
}

添加onclick选项后

imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Class c=null;
                switch (position){
                    case 0:
                        c=Yenii.class;
                        break;
                    case 1:
                        c=Yeni2.class;
                        break;
                    //and……
                }
                Context context = null;
                Intent intent = new Intent(context,c);
                context.startActivity(intent);
            }
        });

但现在我收到此错误

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.content.Context.getPackageName()'

2 个答案:

答案 0 :(得分:1)

对您的代码进行以下更改:

 @Override
    public Object instantiateItem(ViewGroup container, final int position) { //Needs to be final if you want to access it inside an inner class.
        View itemView = layoutinflater.inflate(R.page_1, container, false);

        ImageView imageView = (ImageView) itemView.findViewById(R.id.resim);
         itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  //launch your activity here.
                  Intent intent = new Intent(context, FullScreenImageActivity.class);
                intent.putExtra("IMAGE_RES_ID", mResources[position]);
                startActivity(intent);
            }
        });
        imageView.setImageResource(mResources[position]);
        container.addView(itemView);
        return itemView;
    }
  

最后在你的FullScreenImageActivity OnCreate方法中:

int resourceID = getIntent().getIntExtra("IMAGE_RES_ID", 0);
image.SetImageResource(resourceId); //This is the imageview in the new activity.

答案 1 :(得分:0)

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Class c=null;
        swtich(position){
           case 0:
            c=a.class;
            break;
           case 1:
            c=b.class;
            break;
            //and……
        }
        Intent intent = new Intent(context,c);
        context.startActivity(intent);
    }
});

完成!