如何从数组列表中检索数据到ImageView?

时间:2017-06-16 18:45:19

标签: android android-studio imageview

我试图从我的数组列表中显示我的imageView中的数据。但是,我正在

  

java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.gorila.sqlite_grid_view / com.example.gorila.AvatarShop.imageView}:java.lang.NullPointerException

我试图制作一个简单的功能,当用户从我的自定义列表中购买图像时,存储图像的索引将被添加到我的数组列表中,然后将其传递到我的imageView活动类,然后用户将能够按下用户可以切换到新购买图像的按钮。

因此,它将来自MainActivity - > DetailActivity - > ImageView的。

主要活动:

int[] avatarImage = {R.mipmap.blademaster,R.mipmap.gunslinger,R.mipmap.pyro,R.mipmap.darthvader};
listView = (ListView) findViewById(R.id.avatarlv);

MyAdapter myAdapter = new MyAdapter(MainActivity.this,titles,harga,avatarImage);
listView.setAdapter(myAdapter);

listView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
        Intent mIntent = new Intent(MainActivity.this,DetailActivity.class);
        mIntent.putExtra("title",titles[position]);
        mIntent.putExtra("avatarImages",avatarImage[position]);
        mIntent.putExtra("harga",harga[position]);
        startActivity(mIntent);
    }
});

我的详细活动:

final ArrayList<Integer> list = new ArrayList<Integer>();

btnYes.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        if (mymoney > harga ) { // when my money is > price of the item
            list.add(mBundle.getInt("avatarImages"));
            Toast.makeText(DetailActivity.this, "Successfully Bought the avatar", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(DetailActivity.this, imageView.class);
            intent.putExtra("list1", list);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        else {
            Toast.makeText(DetailActivity.this, "Your money is not enough", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(DetailActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
});

我的ImageView:

private int current_image_index;

ArrayList<Integer> imageBought =  (ArrayList<Integer>)getIntent().getSerializableExtra("list1");

    private void buttonClick()
    {
        imgView  = (ImageView) findViewById(R.id.imageView);
        buttonSw = (Button) findViewById(R.id.buttonSw);
        buttonSw.setOnClickListener(
            new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    current_image_index++;
                    current_image_index = current_image_index % imageBought.size();
                    imgView.setImageResource(imageBought.get(current_image_index));
                }
            }
        );
    }

请帮忙。

1 个答案:

答案 0 :(得分:0)

ImageView.class不是一项活动,因此您无法使用MainActivity.classDetailActivity.class这样的意图来调用它。此外,请在将来发布您的完整错误日志,以便每个人都能获得有关您的错误的更多背景信息。