【问题标题】:Why I can't add an ImageView in the tabbed page?为什么我不能在标签页中添加 ImageView?
【发布时间】:2019-09-05 15:15:45
【问题描述】:

所以我最近才开始通过 Xamarin 开发 Android 应用程序,但我遇到了很多困难。 我这样创建了我的应用程序(在 Visual Studio 2017 社区版中):
新项目 -> Visual C# -> Android -> Android App(Xamarin) -> 选项卡式应用程序(Android 5.0 版)。

然后我将 ImageView 拖到屏幕上的任意位置。我尝试运行它,但它崩溃了,但出现异常:

Unhandled Exception:

Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class android.support.design.widget.BottomNavigationView

我尝试了什么:
1) 在新的 Visual Studio 版本 - 2019 中执行此操作。
2)把它放在一个空白的应用程序中 - 它确实有效,但我的应用程序需要标签页功能,所以这不是一个选项。

感谢任何提示和技巧。这里是可视化它的图像。
Code line where it crashes
This is how it looks from the designer perspective.
Error from designer window

MainActivity.cs:

using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;

namespace App10
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity, BottomNavigationView.IOnNavigationItemSelectedListener
    {
        TextView textMessage;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Error on this line.
            SetContentView(Resource.Layout.activity_main);

            textMessage = FindViewById<TextView>(Resource.Id.message);
            BottomNavigationView navigation = FindViewById<BottomNavigationView>(Resource.Id.navigation);
            navigation.SetOnNavigationItemSelectedListener(this);
        }
        public bool OnNavigationItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.navigation_home:
                    textMessage.SetText(Resource.String.title_home);
                    return true;
                case Resource.Id.navigation_dashboard:
                    textMessage.SetText(Resource.String.title_dashboard);
                    return true;
                case Resource.Id.navigation_notifications:
                    textMessage.SetText(Resource.String.title_notifications);
                    return true;
            }
            return false;
        }
    }
}

【问题讨论】:

  • 请不要将代码和错误作为图片发布
  • 你使用的目标框架是什么?它可能是 appcompat 和支持库版本的版本(未匹配)...
  • 代码作为图像不是最好的,因为我们无法复制/粘贴它来帮助解决您的问题,或者在最后一张图像“来自设计器窗口的错误”的情况下,请参阅错误的结尾.
  • 我从其中一张图片中添加了源代码。我无法从设计器窗口复制/粘贴异常错误。
  • @Çöđěxěŕ 在哪里可以找到版本?

标签: java c# android visual-studio xamarin


【解决方案1】:

然后我将 ImageView 拖到屏幕上的任意位置。我尝试运行它,但它崩溃并出现异常:

经过测试,我发现这不是你的问题,这应该是VS的一个bug。

解决方案:

修改axml文件后,需要清理项目,然后重新构建。不会发生此错误。

Clean Solution -&gt; Build Solution可以解决。

【讨论】:

  • 谢谢先生,这解决了我的问题,但是 VS 的人应该尽快修复它。也许在 github repo 页面中解决它?
  • @TheFunnySmell 是的,你可以在 github 中找到它。记得在这里分享 github 链接:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 2016-05-04
  • 2011-03-18
  • 1970-01-01
  • 2022-01-17
  • 2017-11-13
  • 2020-10-17
相关资源
最近更新 更多