簡體   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