- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 SpotOnView
的 View ,它正在 Game_shooting_AB
上扩展。编码如下。
在 SpotOnView 中有一个倒计时器。时间到,游戏结束,弹出对话框询问用户是退出还是重新玩。
public class SpotOnView extends View
{
......
// constructs a new SpotOnView
public SpotOnView(Context context, RelativeLayout parentLayout)
{
super(context);
resources = context.getResources();
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
....
}
public void start_timer()
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
}
}
public class MyCountDownTimer extends CountDownTimer
{
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
text.setText("Time's up!");
((Game_shooting_AB)getContext()).replay_dialog(); //LINE 181
}
@Override
public void onTick(long millisUntilFinished)
{
....
}
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_shooting);
// create a new SpotOnView and add it to the RelativeLayout
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout);
view = new SpotOnView(this, layout); //EXTENDING HERE
layout.addView(view, 0); // add view to the layout
....
}
public void replay_dialog()
{
final Dialog dialog1 = new Dialog(Game_shooting_AB.this, android.R.style.Theme_Translucent_NoTitleBar);
WindowManager.LayoutParams lp = dialog1.getWindow().getAttributes();
lp.dimAmount = 0.7f;
dialog1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Window window = dialog1.getWindow();
window.setGravity(Gravity.CENTER);
dialog1.setContentView(R.layout.alert_dialog_replay_shooting);
dialog1.setCancelable(false);
dialog1.show(); //LINE 431
....
必要的行号已在上述代码中添加为注释。
11-22 00:16:34.365: W/dalvikvm(1105): threadid=1: thread exiting with uncaught exception (group=0x41f342a0)
11-22 00:16:34.370: E/AndroidRuntime(1105): FATAL EXCEPTION: main
11-22 00:16:34.370: E/AndroidRuntime(1105): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@43304558 is not valid; is your activity running?
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.view.ViewRootImpl.setView(ViewRootImpl.java:708)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.view.Window$LocalWindowManager.addView(Window.java:554)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.app.Dialog.show(Dialog.java:277)
11-22 00:16:34.370: E/AndroidRuntime(1105): at com.app.abc.Game_shooting_AB.replay_dialog(Game_shooting_AB.java:431)
11-22 00:16:34.370: E/AndroidRuntime(1105): at com.app.abc.SpotOnView$MyCountDownTimer.onFinish(SpotOnView.java:181)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.os.Looper.loop(Looper.java:137)
11-22 00:16:34.370: E/AndroidRuntime(1105): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-22 00:16:34.370: E/AndroidRuntime(1105): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 00:16:34.370: E/AndroidRuntime(1105): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 00:16:34.370: E/AndroidRuntime(1105): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-22 00:16:34.370: E/AndroidRuntime(1105): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-22 00:16:34.370: E/AndroidRuntime(1105): at dalvik.system.NativeStart.main(Native Method)
logcat 显示 你的 Activity 在运行吗?
我知道对话框需要有基础才能显示。但是如何修改代码呢?
是否可以在 SpotOnView
中编码并显示对话框,而不是在 Game_shooting_AB
代码中编写?
我尝试通过将类 MyCountDownTimer 和方法 replay_dialog 移回 Game_shooting_AB 来修改代码,但仍然出现相同的错误。请问 MyCountDownTimer 的 OnFinish 是否会导致 Activity 结束,以至于对话框无法找到要显示的基数?
非常感谢!!
最佳答案
问题从“((Game_shooting_AB)getContext()).replay_dialog();”开始这行代码。
当您从 View 内部调用 getContext() 时,您会从 android 文档中找到,您会得到“ View 的上下文”。现在您正在做的是将该上下文转换为 Game_shooting_AB,这并不好,因为它可能是也可能不是 Activity 的上下文。结帐Romain Guy's answer对这个。因此 android 会告诉您“您的 Activity 正在运行吗?”。
现在要解决您当前的问题,您可以通过任何公共(public)方法将您的 Activity 实例传递给此 View ,并将其存储在局部变量中。
像这样调整你的代码:
public class SpotOnView extends View
{
Game_shooting_AB mGame_shooting_AB;
public void setGame_shooting_AB(Game_shooting_AB mGame_shooting_AB){
this.mGame_shooting_AB = mGame_shooting_AB;
}
......
// constructs a new SpotOnView
public SpotOnView(Context context, RelativeLayout parentLayout)
{
super(context);
resources = context.getResources();
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
....
}
public void start_timer()
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
}
}
public class MyCountDownTimer extends CountDownTimer
{
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
text.setText("Time's up!");
mGame_shooting_AB.replay_dialog(); //LINE 181
}
@Override
public void onTick(long millisUntilFinished)
{
....
}
}
获得 SpotOnView 实例后设置 Activity :
view = new SpotOnView(this, layout); //EXTENDING HERE
layout.addView(view, 0); // add view to the layout
view.setGame_shooting_AB(Game_shooting_AB.this);
希望这对您有所帮助。
关于机器人:你的 Activity 在运行吗? OnFinish 时无法显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20126762/
我想做的就是在按下主页按钮时调用 onFinish(),我想这样做所以我总是在打开我的应用程序时进入主要 Activity ,我不希望用户跳回到他离开的地方(我知道在大多数情况下这是一个糟糕的概念,但
如何停止 for 循环并等到方法完成并在调用 onFinish() 后继续? 我能想到的一个解决方案是使用 listeners 并在方法调用 onFinish() 时调用它,但我不知道如何停止循环并等
我想在单击按钮时取消 countdountimer 的 onfinish 函数我执行 timer.cancel 但仍然调用 on finish。 running = false
更新:2013 年 11 月 20 日:此问题仍未解决。 我正在制作一个 CountDownTimer 并且在 onFinish() 方法中,我显然做了太多的工作,因为最后一秒和完成之间的延迟时间比1
是否有方法或属性可以让 CountDownTimer 变为 0 并强制执行 onFinish() 方法? 想象一下:传球游戏有一个计时器和一个“传球”按钮。一旦玩家完成了他/她需要做的事情,他/她就可
我在 HTML 中使用 SoundManager2 和 'ui360/ThreeSixtyPlayer' 样式。我有一个表和一系列 anchor ,其适当的 href 设置为音频流。 当页面呈现时,我
在我的元素中,我实现了一个适用于 jQuery 的代码。 代码如下: $("#startProgressTimer").click(function() {
当我完成我的应用程序时,出现此异常 5 秒(大约): 04-24 20:05:17.293 ... E/Surface: getSlotFromBufferLocked: unknown buffer
我有三项 Activity (a、b、c)。 Activity a - 有一个用于 Activity b 的按钮和一个用于关闭应用程序的按钮(通过调用 onfinish())。 - 是主要 Activ
如何获取 onFinish 中运行的函数当前在 Shadowbox 中显示的图像的 URL?我正在尝试使用 EXIF 信息填充 div,我可以使用 PHP 完成该部分,但我不确定如何在 Shadowb
我使用 jquery.steps,但在按下完成按钮后我无法禁用它。 这是我对组件的初始化: $("#wizard").steps({ onFinished: function (eve
我有一个名为 SpotOnView 的 View ,它正在 Game_shooting_AB 上扩展。编码如下。 背景: 在 SpotOnView 中有一个倒计时器。时间到,游戏结束,弹出对话框询问用
我试图创建一个简单的 Tap Counter 应用程序,但在它移动到 onFinish() 之前我在最后发现了一个明显的滞后,在停止计数器之前给用户一些额外的点击。 这是MainActivity.ja
我在我的代码中使用 CountDownTimer,当我运行应用程序时,我遇到一个问题,即当我进入下一个级别(进入下一个 Activity )时,计时器应该停止并且不运行 onfinish 方法,但是当
我正在编写一项 HIIT(高强度间歇训练) Activity ,我正在为其实现间隔计时器。 CountDownTimer 应该完成 5 分钟的热身,然后继续为 HIIT 锻炼计时。 public c
我正在使用以下代码从我的服务器在 WebView 中加载 pdf: webView = UIWebView(frame: CGRectMake(0, y, screenSize.width,
我是 React 的初学者,我正在学习如何使用 Django 后端创建 React 应用程序的教程。在视频中,他使用了 Ant Design Components v3(这是制作视频时的最新版本)。现
如何使用 onFinish() 函数将 Vimeo 嵌入视频重定向到另一个 URL? 我尝试过 function onFinish() { window.location = "ht
我对“countDownTimer”有一个问题,当我在 ListView 中的两个不同行中运行两个计数器时,“onfinish”函数仅对最后单击的行进行.. 因此,“onfinish”方法使对话框显示
我想做的是使用 CountDownTimer 类中的 onFinish() 方法通过 Intent 传递值。 Intent 的流动是这样的。我的 MyCountDownTimer 类包含将传递 Int
我是一名优秀的程序员,十分优秀!