与 Handler 相关的多个编译错误

Posted

技术标签:

【中文标题】与 Handler 相关的多个编译错误【英文标题】:Multiple compilation errors related to Handler 【发布时间】:2021-12-03 15:28:32 【问题描述】:

复制源代码后有一些错误无法解决。谁能知道我该怎么办?

以下是错误:

无法解析“处理程序”中的方法“sendMessage” 类“从处理程序派生的匿名类”必须声明为抽象或在“处理程序”中实现抽象方法“publish(LogRecord)”

MusicPlayer.java

package com.example.serenityapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.logging.Handler;

public class MusicPlayer extends AppCompatActivity 

ImageView play, prev, next, imageView;
TextView songTitle;
SeekBar mSeekBarTime, mSeekBarVol;
static MediaPlayer mMediaPlayer;
private Runnable runnable;
private AudioManager mAudioManager;
int currentIndex = 0;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_player);

    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);

    // initializing views

    play = findViewById(R.id.play);
    prev = findViewById(R.id.prev);
    next = findViewById(R.id.next);
    songTitle = findViewById(R.id.songTitle);
    imageView = findViewById(R.id.imageView);
    mSeekBarTime = findViewById(R.id.seekBarTime);
    mSeekBarVol = findViewById(R.id.seekBarVol);

    // creating an ArrayList to store our songs

    final ArrayList<Integer> songs = new ArrayList<>();

    songs.add(0, R.raw.sound1);
    songs.add(1, R.raw.sound2);
    songs.add(2, R.raw.sound3);
    songs.add(3, R.raw.sound4);
    songs.add(4, R.raw.sound5);


    // intializing mediaplayer

    mMediaPlayer = MediaPlayer.create(getApplicationContext(), songs.get(currentIndex));



    // seekbar volume

    int maxV = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int curV = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    mSeekBarVol.setMax(maxV);
    mSeekBarVol.setProgress(curV);

    mSeekBarVol.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 

            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) 

        

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) 

        
    );


    //above seekbar volume
    //

    play.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            mSeekBarTime.setMax(mMediaPlayer.getDuration());
            if (mMediaPlayer != null && mMediaPlayer.isPlaying()) 
                mMediaPlayer.pause();
                play.setImageResource(R.drawable.ic_playbtn_foreground);
             else 
                mMediaPlayer.start();
                play.setImageResource(R.drawable.ic_pausebtn_foreground);
            

            songNames();

        
    );


    next.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            if (mMediaPlayer != null) 
                play.setImageResource(R.drawable.ic_pausebtn_foreground);
            

            if (currentIndex < songs.size() - 1) 
                currentIndex++;
             else 
                currentIndex = 0;
            

            if (mMediaPlayer.isPlaying()) 
                mMediaPlayer.stop();
            

            mMediaPlayer = MediaPlayer.create(getApplicationContext(), songs.get(currentIndex));

            mMediaPlayer.start();
            songNames();

        
    );


    prev.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 

            if (mMediaPlayer != null) 
                play.setImageResource(R.drawable.ic_pausebtn_foreground);
            

            if (currentIndex > 0) 
                currentIndex--;
             else 
                currentIndex = songs.size() - 1;
            
            if (mMediaPlayer.isPlaying()) 
                mMediaPlayer.stop();
            

            mMediaPlayer = MediaPlayer.create(getApplicationContext(), songs.get(currentIndex));
            mMediaPlayer.start();
            songNames();

        
    );



private void songNames() 
    if (currentIndex == 0) 
        songTitle.setText("Sound 1");
        imageView.setImageResource(R.drawable.gridbg3);
    
    if (currentIndex == 1) 
        songTitle.setText("Sound 2");
        imageView.setImageResource(R.drawable.gridbg3);
    
    if (currentIndex == 2) 
        songTitle.setText("Sound 3");
        imageView.setImageResource(R.drawable.gridbg3);
    
    if (currentIndex == 3) 
        songTitle.setText("Sound 4");
        imageView.setImageResource(R.drawable.gridbg3);
    
    if (currentIndex == 4) 
        songTitle.setText("Sound 5");
        imageView.setImageResource(R.drawable.gridbg3);
    


    // seekbar duration
    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() 
        @Override
        public void onPrepared(MediaPlayer mp) 
            mSeekBarTime.setMax(mMediaPlayer.getDuration());
            mMediaPlayer.start();
        
    );

    mSeekBarTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
            if (fromUser) 
                mMediaPlayer.seekTo(progress);
                mSeekBarTime.setProgress(progress);
            
        

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) 

        

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) 

        
    );

    new Thread(new Runnable() 
        @Override
        public void run() 
            while (mMediaPlayer != null) 
                try 
                    if (mMediaPlayer.isPlaying()) 
                        Message message = new Message();
                        message.what = mMediaPlayer.getCurrentPosition();
                        handler.sendMessage(message); //**ERROR IN THIS PART**
                        Thread.sleep(1000);
                    
                 catch (InterruptedException e) 
                    e.printStackTrace();
                
            
        
    ).start();

 //**ERROR IN THIS PART**
@SuppressLint("Handler Leak") Handler handler = new Handler () 
    public void handleMessage  (Message msg) 
        mSeekBarTime.setProgress(msg.what);
    
;

【问题讨论】:

Source code that I copied 复制你不懂的代码是创建你自己不知道如何解决的错误的最简单方法:) 我认为您输入了错误的Handler。你把java.util.logging.Handler 误认为android.os.Handler 【参考方案1】:

您导入了错误的包。您所期待的Handler 出现在android.os.Handler 中。但是你已经导入了java.util.logging.Handler

导入正确的包将解决这两个错误。

MusicPlayer.java 中,进行以下更改:

package com.example.serenityapplication;

import androidx.appcompat.app.AppCompatActivity;

...

import android.os.Handler;

【讨论】:

以上是关于与 Handler 相关的多个编译错误的主要内容,如果未能解决你的问题,请参考以下文章

与证书相关的 SBT 项目编译错误

解决用cmake编译与opencv相关的工程出现的错误

Windows 7使用boost asio MinGW编译错误问题,怎么解决

[Android源码分析] - 异步通信Handler机制

为啥编译时出现多个定义错误?

由于多个重新定义错误,CUDA 样本无法编译