1
我正在用Java和XML構建Android應用程序。應用程序的用戶必須能夠聽到曲調並暫停播放。所以我製作了一個可以播放和暫停曲調的按鈕。我做了一個函數,其中必須更改ImageView
的src
。然而Eclipse給我一個錯誤在mp.start()
和mp.pause()
,它說:「mp不能解決」。MediaPlayer暫停並播放錯誤
我不知道我現在做錯了什麼,我希望有人能幫助我。
Java代碼:
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class Level01vraag01 extends Activity implements OnClickListener {
int playknopvariable = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vraaglayout);
setupButtonClickListeners();
final MediaPlayer mp = MediaPlayer.create(Level01vraag01.this, R.raw.mcdonalds);
}
private void setupButtonClickListeners() {
ImageView playsoundButton = (ImageView) findViewById(R.id.playsoundbutton);
playsoundButton.setOnClickListener(this);
}
private void klikopplaybutton() {
if (playknopvariable == 0) {
mp.start();
playknopvariable = 1;
playsoundButton.setImageResource(R.drawable.playsoundbutton);
} else {
mp.pause();
playknopvariable = 0;
playsoundButton.setImageResource(R.drawable.pause);
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.playsoundbutton:
klikopplaybutton();
break;
}
XML代碼:
<ImageView
android:id="@+id/playsoundbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/guessbutton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="33dp"
android:src="@drawable/playsoundbutton" />
太棒了!謝謝你,你解決了這個問題,但現在當我嘗試更改imageView的來源時,我在「playsoundButton」出現錯誤。 – 2013-03-10 20:25:59
你能更新你的代碼嗎?併發布堆棧跟蹤? – 2013-03-10 20:26:48
你的意思是錯誤在哪一行?錯誤代碼如下: playsoundButton.setImageResource(R.drawable.pause); 它說「playsoundButton無法解決」 – 2013-03-10 20:29:46