【问题标题】:Trigger method in caller class调用者类中的触发方法
【发布时间】:2014-09-04 09:01:27
【问题描述】:

你好,
我有一个应用程序需要从调用新类的类中调用一个方法。
具体来说,我有一个活动类,在该类中我调用了我已经实现的surfaceView 类。

我现在想在surfaceView 中发生某些事情时调用startActivity(intent)

由于我无法从surfaceView 调用另一个活动,我想调用类中调用sufaceView 类的方法。

到目前为止,我已经尝试过类似的东西:

public void startActivity() {
    Start.lose();
}

Start 是调用者类,我添加了 import com.example.Start; 和我这样调用的 lost 方法:

public static void lose(){
    c.startActivity(new Intent(c,GameOver.class));
}

其中 c 是上下文(具体来说是 applicationContext )。我不得不这样做,因为这个方法是static,这是从静态方法调用活动的唯一方法,但它会崩溃并且不会在logcat 中给出任何错误。

对我可以在这里做什么有什么建议吗?

好的,我的logcat 被窃听了,现在我收到错误消息:

07-14 14:42:43.545: E/AndroidRuntime(13111): FATAL EXCEPTION: Thread-115
07-14 14:42:43.545: E/AndroidRuntime(13111): android.util.AndroidRuntimeException:  Calling startActivity() from outside of an Activity  context requires the  FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
07-14 14:42:43.545: E/AndroidRuntime(13111):    at android.app.ContextImpl.startActivity(ContextImpl.java:856)
07-14 14:42:43.545: E/AndroidRuntime(13111):    at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
07-14 14:42:43.545: E/AndroidRuntime(13111):    at com.example.fishtruck.Start.lose(Start.java:92)
07-14 14:42:43.545: E/AndroidRuntime(13111):    at com.example.fishtruck.GameView.startActivity(GameView.java:314)
07-14 14:42:43.545: E/AndroidRuntime(13111):    at com.example.fishtruck.GameView$GameThread.lose(GameView.java:227)
07-14 14:42:43.545: E/AndroidRuntime(13111):    at com.example.fishtruck.GameView$GameThread.run(GameView.java:208)

【问题讨论】:

  • 无日志崩溃??不可能
  • 现在检查一下,我的 logcat 有问题
  • 很明显,您需要在开始活动时设置标志 FLAG_ACTIVITY_NEW_TASK。
  • 是的,我正在努力寻找方法...

标签: android android-activity surfaceview static-methods


【解决方案1】:

我是这样修复的:

c.startActivity(new Intent(c,GameOver.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

感谢 Kumar 帮助我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 2015-12-19
    相关资源
    最近更新 更多