我想在我的应用程序中添加一个播放按钮(我正在使用 MediaPlayer),但是当我声明我的 MediaPlayer 时,我可能做错了什么。
package com.example.tones;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class TonosSet extends Activity
{
/**
* @see android.app.Activity#onCreate(Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tonos_set);
Intent i = getIntent();
int position = i.getExtras().getInt("id");
MediaPlayer mp;
ImageView imageView = (ImageView) findViewById(R.id.full_image_view2);
TextView textView1 = (TextView)findViewById(R.id.namesong);
TextView textView2 = (TextView)findViewById(R.id.artist);
ImageButton play = (ImageButton) findViewById(R.id.ButtonPlay);
ImageButton stop = (ImageButton) findViewById(R.id.ButtonStop);
ImageButton setringtone = (ImageButton) findViewById(R.id.ButtonRingtone);
switch (position)
{
case 0:
imageView.setImageResource(R.drawable.picture);
textView1.setText("soundd");
textView2.setText("aut");
break;
case 1:
imageView.setImageResource(R.drawable.picture);
textView1.setText("sound");
textView2.setText("aut");
break;
}
play.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mp = MediaPlayer.create(TonosSet.this, R.raw.sound1); //Error
mp.start(); //Error again
}
});
}
}
我的错误是:
Cannot refer to a non-final variable mp inside an inner class defined in a different method
如果我更改MediaPlayer mp;
为final MediaPlayer mp;
,则 eclipse 会显示此错误:
The final local variable cannot be assigned, since it is defined in an enclosing type.
我不知道我的应用程序出了什么问题。我已经学习了很多教程,但没有人和我一样的错误,即使我认为我在做同样的事情。