向活动添加按钮时出错

时间:2016-02-26 07:59:23

标签: java android xml

当我尝试添加b5按钮时,它显示我的错误。 b5按钮是普通按钮,b1和b2是图像按钮。使用图像按钮和放大器是否有任何问题?同一个文件中的普通按钮?

我的活动:

public class Settings extends Activity implements OnClickListener {

    ImageButton b1, b2;
    Button b5;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        b5=(Button) findViewById(R.id.button_ok);
        b1 = (ImageButton) findViewById(R.id.imageButton1);
        b2 = (ImageButton) findViewById(R.id.imageButton2);

        b5.setOnClickListener(this);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);

        // TODO Auto-generated method stub

        if (SoundFlag.music_flag == 1) {    
            b1.setBackgroundResource(R.drawable.ic_av_volume_up);
        }
        else{           
            b1.setBackgroundResource(R.drawable.ic_av_volume_off);
        }

        if (SoundFlag.sound_flag == 1) {
            b2.setBackgroundResource(R.drawable.ic_av_volume_up);
        }
        else{

            b2.setBackgroundResource(R.drawable.ic_av_volume_off);
        }

    }

    @Override
    public void onClick(View v) {

        SoundFlag values= new SoundFlag();

        if (v == b1) {
            if (values.music_flag == 1){            
                values.music_flag=0;        
                b1.setBackgroundResource(R.drawable.ic_av_volume_off);
            }
            else
            {
                values.music_flag=1;
                b1.setBackgroundResource(R.drawable.ic_av_volume_up);
            }  
        }

        else if (v == b2) {             
            if (values.sound_flag== 1) {
                values.sound_flag=0;
                b2.setBackgroundResource(R.drawable.ic_av_volume_off);
            }
            else
            {
                values.sound_flag=1;
                b2.setBackgroundResource(R.drawable.ic_av_volume_up);
            }           
        }

     else if (v==b5){           
            //finish();
        }   
    }
} 

1 个答案:

答案 0 :(得分:0)

R.id.button_ok中没有ID为R.layout.settings的按钮。