来自activity.java的setImageResources从另一个activity.java更改图像资源

时间:2014-12-06 16:54:15

标签: java android

我正在制作两个活动,FirstActivity.java,它包含一个带有onClickListener的listView,它启动SecondActivity.java并在单击时更改包含在SecondActivity.java中的ImageView的图像资源。我在FirstActivity.java中设置了这段代码:

Button button1 = (Button) findViewById(R.id.button1)
final ImageView image1 = (ImageView) findViewById(R.id.image1)
button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(FirstActivity.this,SecondActivity.class);
            startActivity(i);
            image1.setImageResource(R.drawable.imagexxx);
        }
    });
}

但是当我运行应用程序并按下按钮时,应用程序崩溃了。我做错了吗?

1 个答案:

答案 0 :(得分:0)

    Button button1 = (Button) findViewById(R.id.button1) 
    final ImageView image1 = (ImageView) findViewById(R.id.image1)
    button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //set Image
        image1.setImageResource(R.drawable.imagexxx);
         //then start another activity
        Intent i = new Intent(FirstActivity.this,SecondActivity.class);
        startActivity(i);

    }
});