Android Fragments 为视图页中的每个视图启动一个活动(已解决和已关闭)

wgxvkvu9  于 2022-12-13  发布在  Android
关注(0)|答案(2)|浏览(142)

你好,我有一个ViewPagerFragment显示一些图像。我想开始一个新的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()”

zwghvu4y

zwghvu4y1#

对代码进行以下更改:

@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.
fwzugrvs

fwzugrvs2#

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);
    }
});

已完成!

相关问题