尝试从资源加载样式时 Xamarin.Android 崩溃 [英] Xamarin.Android Crashes When Trying To Load Style From Resource

查看:20
本文介绍了尝试从资源加载样式时 Xamarin.Android 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xamarin 的另一天和另一个问题!

因此,空白 Xamarin.Android 项目.在资源下的 mipmap-anydpi-v26 文件夹中创建了自定义样式/形状,因为如果我在其他地方或什至在另一个新子文件夹中创建它,则无法访问它.即使解决方案中的任何地方都没有 drawable 文件夹(这对我来说非常令人费解.)

无论如何,这是它的外观:

myshape.xml

<solid android:color="#ffffff"/><size android:height="10dp" android:width="10dp"/><corners android:radius="130dp"/></形状>

然后我将它设置为 LinearLayoutbackground 如下:

 

它完美地体现在设计师身上.即使在模拟器中,它在调试和发布模式下也能很好地工作.

当我尝试在物理设备 (Samsung Galaxy J2) 中调试时出现 Thr 问题.它运行 Android 5.1(API 级别 22)(请注意,我的项目的最低要求是 API 级别 21).因此,当我尝试通过设备调试应用程序时,出现以下错误:

<块引用>

Java.Lang.Exception:二进制 XML 文件第 1 行:错误膨胀类

MainActivity.csOnCreate() 方法中的以下行中引发错误:

SetContentView(Resource.Layout.activity_main);

但是,如果我将背景设置为 ic_launcher,它看起来是一个徽标,但存储为 mipmap-anydpi-v26 中的 .xml 文件 文件夹,它完美地工作.ic_launcher.xml 看起来像这样:

那么,我对如何将形状设置为背景感到有些困惑?我已经浏览了其他 SO 帖子,但几乎所有帖子似乎都有 drawable 文件夹,在我的案例中缺少该文件夹.即使在新项目中,该文件夹也不见了.您可以通过创建一个空白的 Xamarin.Android 项目来简单地重现它.

那么,关于它为什么会在物理设备上崩溃的任何想法?

解决方案

...有我的案例中缺少的 drawable 文件夹.

只需在您的 Resources 文件夹中创建一个 drawable 文件夹即可.

注意:通过 IDE 的解决方案/项目资源管理器执行此操作,而不是文件系统.

有许多资源文件夹,默认情况下 Xamarin 模板不会创建所有这些文件夹.

注意:它们实际上变成了 Android APK 文件结构中的 res/xxxx.

re:

Another day with Xamarin and another issue!

So, blank Xamarin.Android project. Created a custom style/shape in the mipmap-anydpi-v26 folder under resources because it's not accessible if i create it elsewhere or even in another new sub-folder. Even there's no drawable folder anywhere in the solution(which is very puzzling for me.)

Anyways, here's how it looks :

myshape.xml

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle">
  <solid android:color="#ffffff" />
  <size android:height="10dp" android:width="10dp"/>
  <corners android:radius="130dp" />
</shape>

Then i set it as a background of a LinearLayout as follows :

 <LinearLayout android:layout_height="match_parent"
               android:layout_weight="match_parent"
               android:background="@mipmap/myshape"/>

It shows perfectly within the designer. Even within the emulator, it works great in both debug and release mode.

Thr problem arises when i try to debug it in a physical device(Samsung Galaxy J2). It runs Android 5.1(API Level 22) (Note that my project's minimum requirement is API level 21). So, when i try to debug the app through the device, i get the following error :

Java.Lang.Exception: Binary XML file line #1: Error inflating class

Error is throws in the following line in MainActivity.cs's OnCreate() method :

SetContentView(Resource.Layout.activity_main);

However, if i set the background as ic_launcher which appears to be a logo, but is stored as an .xml file in mipmap-anydpi-v26 folder, it works perfectly. The ic_launcher.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
 <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
 <background android:drawable="@color/ic_launcher_background"/>
 <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

So, i am a bit confused as how do i set a shape as a background then? I have gone through other SO posts but almost all of them seem to have the drawable folder which is missing in my case. Even in a new project, the folder is missing. You can simply reproduce it by creating a blank Xamarin.Android project.

So, any ideas on why it may be crashing on on a physical device?

解决方案

...have the drawable folder which is missing in my case.

Just create a drawable folder within your Resources folder.

Note: Do this via the IDE's solution/project explorer, not the file-system.

There are a number of Resources folders and Xamarin templates do not create all of them by default.

Note: They actually become res/xxxx within the Android APK file structure.

re: https://developer.android.com/training/multiscreen/screendensities

这篇关于尝试从资源加载样式时 Xamarin.Android 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆