0

I have this code:

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    MediaPlayer sound1, sound2;
    sound1 = MediaPlayer.create(this, R.raw.cows);
    sound2 = MediaPlayer.create(this, R.raw.sheep);

    final Button button1 = (Button) findViewById(R.id.Button01);
    button1.setOnClickListener(this);

    final Button button2 = (Button) findViewById(R.id.Button02);
    button1.setOnClickListener(this);

    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.Button01:
                sound1.start();
                break;
            case R.id.Button02:
                sound2.start();
                break;
        }
    }

    protected void onDestroy() {
        sound1.release();
        sound2.release();
        super.onDestroy();
    }
}

I have a warning that says that the button and the view are not correct.
However, I don't understand what's wrong with the above code.
It seems that I need to instantiate the button class and the View class.
But I don't know how to do that.

0

3 Answers 3

3

This should be inside a method

sound1 = MediaPlayer.create(this, R.raw.cows);
sound2 = MediaPlayer.create(this, R.raw.sheep);


final Button button1 = (Button) findViewById(R.id.Button01);
button1.setOnClickListener(this);

final Button button2 = (Button) findViewById(R.id.Button02);
button1.setOnClickListener(this); // should be button2

You can initialize your views in onCreate

Button button1,button2;
MediaPlayer sound1,sound2;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sound1 = MediaPlayer.create(this, R.raw.cows);
sound2 = MediaPlayer.create(this, R.raw.sheep);

button1 = (Button) findViewById(R.id.Button01);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.Button02);
button2.setOnClickListener(this);
}

Then

public class MainActivity extends Activity implements onClickListener {
Sign up to request clarification or add additional context in comments.

Comments

2
  • First you have to put `final Button button1 = (Button) findViewById(R.id.Button01); button1.setOnClickListener(this);

    final Button button2 = (Button) findViewById(R.id.Button02); button1.setOnClickListener(this); ` in onCreate() method.

  • Them write (implements OnClickListener after extends Activity )that will ask to implement methods click that

That will automatically create onclick method. in that put you switch code in it.

Hope It Will works and Help you to solve error.

Comments

0

Implement View.onClickListenerand above the ònClick` method you should include the @Override notation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.