0

我正在尝试从服务中关闭进度条,但显示强制关闭错误。我已经在 startservice onclick 按钮的主要活动中创建了进度条,并且我想在为 mediaplyer 准备监听器时将其关闭。由于我们无法在服务中编写 ui 函数,因此我将其写入活动中。我也试过 Main_acitivty object 。pregressbar.dismiss(); 但它显示强制关闭错误。这个问题的任何好的解决方案。

已编辑:-我有进度条,但是当准备完成并且媒体播放器即将启动时,由于这条线我认为“vm.progressDialog.dismiss();”而出现强制关闭错误 玩();

答案:- 我将活动导入服务,在活动中我将进度条设置为静态进度条,它工作正常。

这是我的服务,媒体播放器已初始化

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


 mp = new MediaPlayer();
    try {
        showNoti();
        final Main vm = new Main();


        mp.setDataSource(link);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.prepareAsync();

        mp.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                // TODO Auto-generated method stub
                vm.progressDialog.dismiss();
                mp.start();
                b=true;
            }
        });

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

错误

09-17 18:12:01.330: E/AndroidRuntime(6708): FATAL EXCEPTION: main
09-17 18:12:01.330: E/AndroidRuntime(6708): java.lang.NullPointerException
09-17 18:12:01.330: E/AndroidRuntime(6708): at com.android.twitter.myPlayService$1.onPrepared(myPlayService.java:53)
09-17 18:12:01.330: E/AndroidRuntime(6708): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:1973)
09-17 18:12:01.330: E/AndroidRuntime(6708): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 18:12:01.330: E/AndroidRuntime(6708): at android.os.Looper.loop(Looper.java:137)
4

1 回答 1

0

更好的解决方案是让你的进度条只在活动中,并注册一个BroadcastReceiver关闭进度条的。

内部服务:

  mp.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
         intent.setAction("dismiss_progress_dialog");
       sendBroadcast(intent);

            mp.start();
            b=true;
        }
    });

内部活动

 class MyBroadcast extends BroadcastReceiver {
            @Override
            public void onReceive(Context ctxt, Intent i) {


                progressDialog.dismiss();

            }
        };

  MyBroadcast receiver;

在 onResume 活动中:

    receiver=new MyBroadcast();
    IntentFilter filter = new IntentFilter();
    filter.addAction("dismiss_progress_dialog");

    registerReceiver(receiver, filter);

在 onPause 里面:

   unregisterReceiver(receiver);
于 2014-07-15T11:27:14.180 回答