繁体   English   中英

活动需要 FLAG_ACTIVITY_NEW_TASK 标志

[英]Activity requires FLAG_ACTIVITY_NEW_TASK flag

我正在使用 Xamarin,当我单击具有电话号码链接的 TextView 时,我的模拟器正在执行错误。

应用程序输出具有以下输出:

[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?

我需要在哪里设置标志,我应该将它设置到什么位置?

我可以帮忙吗?

提前致谢。

编辑

这是我的应用程序代码:

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);
        }
    }
}

在上面的代码中,我应该在哪里放置 Intent 标志?

编辑2

这是我使用 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);
        }
    }
}

但是,现在发生此错误:

android.util.SuperNotCalledException:活动 {TestTextViewAutoLink.TestTextViewAutoLink/testtextviewautolink.MainActivity} 没有调用到 super.onCreate()

我怎样才能让这个代码工作?

您错过了在超类中编写 onCreate 函数的调用

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);
        }

尝试这个..

错误本身就有意义

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

例子

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

应避免设置标志,因为它会干扰事件和历史堆栈的正常流动。

因此,要在 Xamarin.Android 项目中获取 CurrentActivity,请使用:

Xamarin.Essentials.Platform.CurrentActivity

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

请注意,这可能需要 Xamarin.Essentials v1.5 或更高版本

暂无
暂无

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

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