简体   繁体   English

活动需要 FLAG_ACTIVITY_NEW_TASK 标志

[英]Activity requires FLAG_ACTIVITY_NEW_TASK flag

I am using Xamarin and my emulator is performing an error when I click on a TextView that has a link for a phone number.我正在使用 Xamarin,当我单击具有电话号码链接的 TextView 时,我的模拟器正在执行错误。

The application output has this output:应用程序输出具有以下输出:

[MessageQueue-JNI] Exception in MessageQueue callback: handleReceiveCallback
[MessageQueue-JNI] android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Where do I need to set the flag, and what should I set it to?我需要在哪里设置标志,我应该将它设置到什么位置?

May I please have some help to get this working?我可以帮忙吗?

Thanks in advance.提前致谢。

EDIT编辑

Here is my application code:这是我的应用程序代码:

namespace TestTextViewAutoLink
{
    [Activity (Label = "TestTextViewAutoLink", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            TextView textView = new TextView (this.ApplicationContext);
            textView.AutoLinkMask = Android.Text.Util.MatchOptions.PhoneNumbers; 
            textView.Text = "This is a phone number 0800 32 32 32";

            //Linkify.AddLinks(textView, MatchOptions.PhoneNumbers);

            SetContentView(textView);
        }
    }
}

Where, in the above code, should I place the Intent flag?在上面的代码中,我应该在哪里放置 Intent 标志?

EDIT2编辑2

Here is my code to start the activity, with ActivityFlags.NewTask这是我使用 ActivityFlags.NewTask 启动活动的代码

namespace TestTextViewAutoLink
{
    [Activity (Label = "TestTextViewAutoLink", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            Intent intent= new Intent(this.ApplicationContext, typeof(AutoLinkActivity));
            intent.SetFlags(ActivityFlags.NewTask);
            StartActivity(intent);
        }
    }
}

However, this error now occurs:但是,现在发生此错误:

android.util.SuperNotCalledException: Activity {TestTextViewAutoLink.TestTextViewAutoLink/testtextviewautolink.MainActivity} did not call through to super.onCreate() android.util.SuperNotCalledException:活动 {TestTextViewAutoLink.TestTextViewAutoLink/testtextviewautolink.MainActivity} 没有调用到 super.onCreate()

How can I get this code working?我怎样才能让这个代码工作?

You missed to write call for onCreate function in super class您错过了在超类中编写 onCreate 函数的调用

base.OnCreate (bundle); base.OnCreate(捆绑);

protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            Intent intent= new Intent(this.ApplicationContext, typeof(AutoLinkActivity));
            intent.SetFlags(ActivityFlags.NewTask);
            StartActivity(intent);
        }

Try this..尝试这个..

The Error itself having meaning错误本身就有意义

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

Example例子

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Setting flags should be avoided as it will interfere with normal flow of event and history stack.应避免设置标志,因为它会干扰事件和历史堆栈的正常流动。

Hence to get CurrentActivity in Xamarin.Android Project use:因此,要在 Xamarin.Android 项目中获取 CurrentActivity,请使用:

Xamarin.Essentials.Platform.CurrentActivity

//Now you can start an activity as
Xamarin.Essentials.Platform.CurrentActivity.StartActivity(intent);

Note that this might require Xamarin.Essentials v1.5 or above请注意,这可能需要 Xamarin.Essentials v1.5 或更高版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM