【问题标题】:Android start activity shows errorAndroid 启动活动显示错误
【发布时间】:2017-01-19 10:41:44
【问题描述】:

当我开发一个安卓应用程序时。我想以编程方式拨打电话。关于使用

Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
getContext().startActivity(callIntent);

它显示一个错误。我使用的是 FrameLayout 而不是 Activity。

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1366)
at android.app.ContextImpl.startActivity(ContextImpl.java:1353)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:322)
at com.happiness.lockscreenlibrary.view.LockView$1.onDrag(LockView.java:163)
at android.view.View.dispatchDragEvent(View.java:18339)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1492)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1478)
at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1478)
at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:5143)
at android.view.ViewRootImpl.access$700(ViewRootImpl.java:108)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3331)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

我使用了context.getApplicationContext().startActivity(callIntent);context.startActivity(callIntent); 而不是getContext().startActivity(callIntent); 但它显示了同样的错误。如何解决,请帮助我。

【问题讨论】:

  • Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

标签: android android-activity android-framelayout android-phone-call


【解决方案1】:

要从服务或除其他活动之外的任何地方启动活动,您必须将标志 FLAG_ACTIVITY_NEW_TASK 添加到意图。

Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
callIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(callIntent);

【讨论】:

    【解决方案2】:

    您必须添加 setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

    如果设置,此活动将成为此活动的新任务的开始 历史堆栈。

    Uri number = Uri.parse("tel:123456789");
    Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
    callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    getContext().startActivity(callIntent);
    

    【讨论】:

      【解决方案3】:
      Uri number = Uri.parse("tel:123456789");
      Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
      callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(callIntent);
      // use getActivity().startActivity(callIntent); if you are inside a fragment
      

      您可以在此处获取有关意图标志的信息:

      https://developer.android.com/reference/android/content/Intent.html

      FLAG_ACTIVITY_NEW_TASK 如果设置,这个活动将成为开始 此历史堆栈上的新任务。

      【讨论】:

        【解决方案4】:

        使用 FLAG_ACTIVITY_NEW_TASK 调用您的 startActivity。 logcat 指出错误,你仍然在 SO 上发布它

        Uri number = Uri.parse("tel:123456789");
        Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
        callIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(callIntent);
        

        【讨论】:

          【解决方案5】:

          尝试在您的意图中添加标志:

          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          

          您的代码将是

          Uri number = Uri.parse("tel:123456789");
          Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
          callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          getContext().startActivity(callIntent);
          

          希望能帮到你!

          【讨论】:

            【解决方案6】:

            您应该使用构造函数将上下文从活动传递到视图。

            new LockView(this);
            

            使用相同的上下文来开始一个新的活动。

               Uri number = Uri.parse("tel:123456789");
                Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(callIntent);
            

            添加Intent.FLAG_ACTIVITY_NEW_TASK 标志以开始新活动。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-12-01
              • 2012-07-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多