App crashes on change of ImageView

后端 未结 2 1636
粉色の甜心
粉色の甜心 2021-01-24 09:14

I\'m trying to create a simple flashlight app in order to learn android development. I\'m trying to get it so that when when you click on the light ImageView object it changes t

2条回答
  •  醉酒成梦
    2021-01-24 10:02

    you do not need to declare a variable light in the handler

    public class flash extends ActionBarActivity {
    
            public Boolean on = false;
            ImageView light;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_flash);
    
                light = (ImageView) findViewById(R.id.light);
    
                light.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        if(on){
                            light.setImageResource(R.drawable.flash_off);
                            on = false;
                        }else{
                            light.setImageResource(R.drawable.flash_on);
                            on = true;
                        }
                    }
                });
            }
    

提交回复
热议问题