1

我正在使用 Java 和 XML 为 Android 构建一个应用程序。应用程序的用户必须能够听一首曲子并暂停它。所以我做了一个按钮,你可以用它来播放和暂停曲子。我做了一个必须改变src的函数。但是 Eclipse 在和ImageView处给了我一个错误,上面写着:"mp cannot be resolved"mp.start()mp.pause()

我不知道我现在做错了什么,我希望有人能帮助我。

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" />
4

1 回答 1

3

那是因为我认为您已经在 onCreate 方法上声明并实例化了 mp ,并且在 klikopplaybutton() 方法中无法访问它。

尝试这样的事情。

public class Level01vraag01 extends Activity implements OnClickListener {

    int playknopvariable = 0;
    MediaPlayer mp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vraaglayout);
        setupButtonClickListeners();
        mp = MediaPlayer.create(Level01vraag01.this, R.raw.mcdonalds);

    }

private void setupButtonClickListeners()
{
    ImageView playsoundButton = (ImageView) findViewById(R.id.playsoundbutton);
    playsoundButton.setOnClickListener(this);
}

更新您的 ImageView 上的错误。

您在 playsoundButton ImageView 对象中遇到错误,因为您再次声明它并在 setupButtonClickListeners 方法中执行了 findViewById,并且在 klikopplaybutton 方法中无法访问它。

试试这个。

public class Level01vraag01 extends Activity implements OnClickListener {

    int playknopvariable = 0;
    MediaPlayer mp;
    ImageView playsoundButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vraaglayout);
        setupButtonClickListeners();
        mp = MediaPlayer.create(Level01vraag01.this, R.raw.mcdonalds);

    }

    private void setupButtonClickListeners(){
        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;
   }

}
于 2013-03-10T20:20:58.917 回答