当我将其设置为全屏时,为什么我的ImageView会隐藏?

时间:2015-01-30 00:47:06

标签: java android

我只是想在全屏中查看小尺寸的照片,所以我添加了另一个ImageView作为名为zoom的全屏视图,当我点击它时它会隐藏。但是,我找不到错误。我已经尝试过可见性更改,似乎没有任何错误:

public class HistoryActivity extends ActionBarActivity {

    ImageView v1 = (ImageView)findViewById(R.id.iv);
    ImageView v2 = (ImageView)findViewById(R.id.iv2);
    ImageView v3 = (ImageView)findViewById(R.id.iv3);
    ImageView v4 = (ImageView)findViewById(R.id.iv4);
    ImageView v5 = (ImageView)findViewById(R.id.iv5);
    ImageView zoom = (ImageView)findViewById(R.id.imageView5);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        v1.setImageResource(R.drawable.pic1);
        v2.setImageResource(R.drawable.pic1);
        v3.setImageResource(R.drawable.pic1);
        v4.setImageResource(R.drawable.pic1);
        v5.setImageResource(R.drawable.pic1);
        zoom.setVisibility(View.INVISIBLE);
        v1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoom.setImageResource(R.drawable.pic1);
                zoom.setVisibility(View.VISIBLE);
            }
        });
        v2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoom.setImageResource(R.drawable.pic1);
                zoom.setVisibility(View.VISIBLE);
            }
        });
        v3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoom.setImageResource(R.drawable.pic1);
                zoom.setVisibility(View.VISIBLE);
            }
        });
        v4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoom.setImageResource(R.drawable.pic1);
                zoom.setVisibility(View.VISIBLE);
            }
        });
        v5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                zoom.setImageResource(R.drawable.pic1);
                zoom.setVisibility(View.VISIBLE);
            }
        });

        ImageButton btnBack = (ImageButton) findViewById(R.id.imageButton6);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(zoom.getVisibility() != View.INVISIBLE) {
                    zoom.setVisibility(View.INVISIBLE);
                }
                else {
                    Intent myIntent = new Intent(v.getContext(), CityActivity.class);
                    startActivityForResult(myIntent, 0);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:4)

这样做,

ImageView v1 = (ImageView)findViewById(R.id.iv);
ImageView v2 = (ImageView)findViewById(R.id.iv2);
ImageView v3 = (ImageView)findViewById(R.id.iv3);
ImageView v4 = (ImageView)findViewById(R.id.iv4);
ImageView v5 = (ImageView)findViewById(R.id.iv5);
ImageView zoom = (ImageView)findViewById(R.id.imageView5);
onCreate 中,setContentView()之后

v1.setImageResource(R.drawable.pic1);
    v2.setImageResource(R.drawable.pic1);
    v3.setImageResource(R.drawable.pic1);
    v4.setImageResource(R.drawable.pic1);
    v5.setImageResource(R.drawable.pic1);

在调用setContentView()之前,无法初始化视图元素。在那之前它们只是空的..