c# - Activity 需要 FLAG_ACTIVITY_NEW_TASK 标志

标签 c# android android-activity textview xamarin

我正在使用 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 标志?

EDIT2

这是我使用 ActivityFlags.NewTask 启动 Activity 的代码

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: Activity {TestTextViewAutoLink.TestTextViewAutoLink/testtextviewautolink.MainActivity} did not call through to super.onCreate()

如何让这段代码正常工作?

最佳答案

您错过了在父类(super class)中编写对 onCreate 函数的调用

base.OnCreate( bundle );

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

关于c# - Activity 需要 FLAG_ACTIVITY_NEW_TASK 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086272/

相关文章:

android - 仅在单击 BottomNavigationView 时显示的项目

android - 如何将 Theme.Holo.DialogWhenLarge 样式设置为自定义大小、位置、边距等?

c# - 不同语言发展的相对成本研究

c# - 如何停止 System.Threading.Timer

c# - 无法在同一 block 中启用已禁用的控件

c# - 在 WPF 的 DoubleAnimation 期间更新 ViewModel 中的属性

android - 应用程序启动时出现异常

android模拟器不会在窗口中打开

java - AlertDialog.Builder 中的 Android stopService()

android-activity - Application 子类中的 startActivity