问题描述:

i have got a method in my code to play music but it isn't quite working. Red underlining in one line... Here's my code:

public class MyActivity2 extends Activity {

private String[] colors;

private String[] values;

private TextView tv;

private RelativeLayout rl;

private MediaPlayer mp;

Button n;

int index = 0;

int position2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my_activity2);

n = (Button) findViewById(R.id.button);

tv = (TextView) findViewById(R.id.textView);

rl = (RelativeLayout) findViewById(R.id.layout_view);

Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");

n.setTypeface(typeface);

Typeface face = Typeface.createFromAsset(getAssets(), "OSP-DIN.ttf");

tv.setTypeface(face);

values = getResources().getStringArray(R.array.allthings_array);

colors = getResources().getStringArray(R.array.colorcode_array);

n.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// mp = new MediaPlayer();

mp = MediaPlayer.create(this, R.raw.pop);

mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

mp.setLooping(true);

mp.start();

}

});

n.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

if (index == 5) {

Toast.makeText(getApplicationContext(), "More coming soon!", Toast.LENGTH_SHORT).show();

} else {

position2 = (index++);

}

String textValue = values[position2];

tv.setText(textValue);

Random RAND = new Random();

int position = RAND.nextInt(colors.length);

String nextValue = colors[position];

rl.setBackgroundColor(Color.parseColor(nextValue));

n.setBackgroundColor(Color.argb(00, 00, 00, 00));

return true;

case MotionEvent.ACTION_UP:

n.setBackgroundColor(Color.argb(00, 00, 00, 00));

return true;

default:

return false;

}

}

});

}

At the line: mp = MediaPlayer.create(this, R.raw.pop); , (this, R.raw.pop) is underlined in red saying it can't resolve method..

How do I fix this?

网友答案:

The first parameter in the MediaPlayer.create() method is the context. You can usually use this as the context when you're in an Activity class. The tricky part here, which is a common mistake with Android, is that this on that line refers to the View.OnClickListener you are defining. It's called an anonymous inner class.

You can solve this by changing the line to

mp = MediaPlayer.create(MyActivity2.this, R.raw.pop);

Full OnClickListener (verified with my own mp3 raw file):

n.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mp = MediaPlayer.create(MyActivity2.this, R.raw.pop);
        mp.setLooping(true);
        mp.start();
    }
});
网友答案:

You should do it like mp = MediaPlayer.create(MyActivity2.this, R.raw.pop); because you're doing it from an inner class and thus this refers to onClickListener and MediaPlayer.create method does not taks such argument.

相关阅读:
Top