繁体   English   中英

无法从异步功能启动新的Intent活动

[英]Can not start new Intent activity from async function

我在Android的xamarin应用程序中有问题。 从我的主要活动中,我调用一个登录活动,该活动通过Microsoft.WindowsAzure.MobileServices插件检查用户和密码输入是否正确。 我得到了答案,但是当我尝试在异步功能中打开一个新活动时,该错误会导致应用程序挂起。 我尝试从异步方法外部访问该目标活动,并且它可以工作。 我的代码:

private async Task<int> GetAdminUser(string userName,string password)
        {
            int userId = -1;

            try
            {
                users = await usersTable.Where (item => item.UserLoginName == userName && item.UserPassword == password).ToListAsync ();

                if(users != null)
                {
                    userId = users[0].UserId;
                }
                else{
                    userId = -1;
                }
            }
            catch(Exception ex) {
                userId = -1;
            }

            return userId;

        }

        private async Task GetAdminUserWrapper(string userName,string password)
        {
            int userId = await GetAdminUser (userName, password);
            if (userId != -1) 
            {
                //new AlertDialog.Builder (this).SetMessage ("User id : " + userId.ToString ()).Create().Show();
                await OpenUserBusinessSalesIntent (userId);
            }
        }

        async Task OpenUserBusinessSalesIntent (int userId)
        {
            try {
                Intent testActivity = new Intent (this, typeof(TestActivity));
                //userSalesActivity.AddFlags (ActivityFlags.NewTask);

                //userSalesActivity.PutExtra ("UserIdBusiness",userId.ToString ());
                //SetResult (Result.Ok,testActivity);
                StartActivity (testActivity);
            } 
            catch (Exception ex) 
            {

            }
        } 

任何帮助都会向Shlomy求助

在UI线程上启动活动:

runOnUiThread(() => {
  Intent testActivity = new Intent (this, typeof(TestActivity));

  StartActivity (testActivity);
});

感谢您的想法,当我获得userId后,尝试在异步函数中启动新的意图时发生崩溃。 解决方案是使用RunOnUiThread并用try catch将其包装在runOnUiThread内以启动意图。 10x Shlomy

暂无
暂无

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

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