我是Android开发的新手,正在开发音频流应用。我最初发送一个Toast消息,以便在流式传输音频服务时发生错误时通知用户。但是,我想用警告对话框替换它。但我得到了一个错误。
这是我以前的代码:
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(this, "There was an error playing the stream, Check your network connection", Toast.LENGTH_LONG).show();
mediaPlayer.reset();
return false;
}
这是我更新的代码,它会抛出错误
public boolean onError(MediaPlayer mp, int what, int extra) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customTitleView = inflater.inflate(R.layout.my_theme, null);
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setCancelable(true);
alertbox.setView(customTitleView);
alertbox.show();
mediaPlayer.reset();
return false;
}
这是错误:
FATAL EXCEPTION: main android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.app.Dialog.show(Dialog.java:277)
at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
at com.musicianfocus.ben.wordedfm.RadioService.onError(RadioService.java:54)
at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2001)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
06-02 08:43:33.065 321-322/? E/SocketStream: readFully was waiting for 716352 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 700160 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 683968 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 667776 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 651584 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 635392 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 619200 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 603008 bytes, got 16192
06-02 08:43:33.089 321-322/? E/SocketStream: readFully was waiting for 586816 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 570624 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 554432 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 538240 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 522048 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 505856 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 489664 bytes, got 16192
06-02 08:43:33.093 321-322/? E/SocketStream: readFully was waiting for 473472 bytes, got 16192
06-02 08:43:33.097 321-322/? E/SocketStream: readFully was waiting for 457280 bytes, got 16192
06-02 08:43:33.097 321-322/? E/SocketStream: readFully was waiting for 441088 bytes, got 161920
06-02 08:43:33.101 321-322/? E/SocketStream: readFully was waiting for 279168 bytes, got 155972
06-02 08:43:33.105 321-322/? E/SocketStream: readFully was waiting for 123196 bytes, got 16192
06-02 08:43:33.105 321-322/? E/SocketStream: readFully was waiting for 107004 bytes, got 16192
06-02 08:43:33.109 321-322/? E/SocketStream: readFully was waiting for 90812 bytes, got 16192
06-02 08:43:33.109 321-322/? E/SocketStream: readFully was waiting for 74620 bytes, got 16192
06-02 08:43:33.109 321-322/? E/SocketStream: readFully was waiting for 58428 bytes, got 16192
06-02 08:43:33.109 321-322/? E/SocketStream: readFully was waiting for 42236 bytes, got 16192
06-02 08:43:33.109 321-322/? E/SocketStream: readFully was waiting for 26044 bytes, got 16192
06-02 08:48:37.733 321-322/? E/SocketStream: readFully was waiting for 716352 bytes, got 16192
06-02 08:48:37.733 321-322/? E/SocketStream: readFully was waiting for 700160 bytes, got 16192
06-02 08:48:37.733 321-322/? E/SocketStream: readFully was waiting for 683968 bytes, got 1619
答案 0 :(得分:0)
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle("Connection Issue")
.setMessage("There was an error playing the stream, Check your network connection")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with on ok
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();