OnClickListener未检测到点击。类未加载

时间:2018-12-30 23:57:46

标签: java android icecast

我是Android和Java开发的新手。我正在尝试制作一个播放Icecast音频流的应用。

我找到了以下代码,并将其复制并编辑到一个名为Audio Stream的类中

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import java.io.IOException;

public class AudioStream extends Activity implements OnClickListener {

    private Button buttonPlay;

    private MediaPlayer player;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeUIElements();

        initializeMediaPlayer();
        Log.d("Classloaded", "Class loaded");
    }

    private void initializeUIElements() {

        buttonPlay = findViewById(R.id.PlayPauseButton);
        buttonPlay.setOnClickListener(this);
    }

    public void onClick(View v) {
        Log.d("onClick", "On Click worked");
        if (v == buttonPlay) {
            startPlaying();
        }
    }

    private void startPlaying() {
        buttonPlay.setEnabled(false);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });

    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource("streamURL");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }
}

运行应用程序时,不会触发Log.d事件,因此我只能假定该类从未加载和实例化。所以我在MainActivity

中添加了以下内容
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AudioStream stream = new AudioStream();
    }
}

我知道我做错了。如何在应用加载时让AudioStream类实例化,以便在按下播放按钮时OnClickListener可以正常工作?

1 个答案:

答案 0 :(得分:1)

代替:

  public void onClick(View v) {
        Log.d("onClick", "On Click worked");
        if (v == buttonPlay) {
            startPlaying();
        }
    }

您应该喜欢:

  public void onClick(View v) {
        Log.d("onClick", "On Click worked");
        if (v.getId == R.id.PlayPauseButton) {
            startPlaying();
        }
    }