启动服务会引发IllegalAccessException [英] Starting a Service throws an IllegalAccessException

查看:181
本文介绍了启动服务会引发IllegalAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Service,它可以接收音频文件并用MediaPlayer播放.这就是我叫我的Service:

I have a Service which takes in an audio file and plays it with MediaPlayer. This is how I call my Service:

private void playAudio(String url) throws Exception {
    Intent music = new Intent(this,MusicService.class);
    music.putExtra("paths", path);
    startService(music);
}

这是我的Service课:

class MusicService extends Service implements OnCompletionListener {
    MediaPlayer mediaPlayer;
    String musicFile;

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle e = intent.getExtras();
        musicFile= e.getString("paths"); 
        try {
            mediaPlayer.prepare(); 
            mediaPlayer.setDataSource(musicFile);
        } catch (IllegalArgumentException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        } catch (IllegalStateException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        } catch (IOException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        }
        if (!mediaPlayer.isPlaying()) {             
            mediaPlayer.start();             
        } 
        return START_STICKY; 
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

Service永远不会执行,Toast永远不会显示,并且MediaPlayer不会播放.

The Service is never getting executed, the Toast is never shown, and the MediaPlayer does not play.

我在清单中这样声明它:

I declare my it in my manifest like this:

<service android:name=".MusicService" android:enabled="true"></service>

我收到一个强制关闭错误,并且在我的日志中出现了这个IllegalAccessException:

I get a force close error, and this IllegalAccessException in my logs:

java.lang.RuntimeException: Unable to instantiate service unjustentertainment.com.MusicService:
    java.lang.IllegalAccessException: access to class not allowed

推荐答案

您得到的异常是因为系统由于无法访问而无法初始化您的服务(调用其构造函数).

The exception you get is because the system could not init your service (call its constructor) because its not accessible.

此处:

...确保班级是 宣布公开...

...Make sure the class is declared public...

您发布的课程不是公开的,因此将其公开.

The class you posted is not public, so make it public.

这篇关于启动服务会引发IllegalAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆