简体   繁体   English

Xamarin.Android尝试从资源加载样式时崩溃

[英]Xamarin.Android Crashes When Trying To Load Style From Resource

Another day with Xamarin and another issue! Xamarin的另一天,另一个问题!

So, blank Xamarin.Android project. 因此,空白Xamarin.Android项目。 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. 在资源下的mipmap-anydpi-v26文件夹中创建了一个自定义样式/形状,因为如果我在其他地方甚至在另一个新的子文件夹中创建它,就无法访​​问。 Even there's no drawable folder anywhere in the solution(which is very puzzling for me.) 甚至在解决方案中的任何地方都没有drawable文件夹(这让我很困惑。)

Anyways, here's how it looks : 无论如何,这是它的外观:

myshape.xml 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 : 然后我将其设置为LinearLayoutbackground ,如下所示:

 <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 ). 当我尝试在物理设备( Samsung Galaxy J2 )中对其进行调试时,会出现问题。 It runs Android 5.1(API Level 22) ( Note that my project's minimum requirement is API level 21 ). 它运行Android 5.1(API级别22)( 请注意,我的项目的最低要求是API级别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 Java.Lang.Exception:二进制XML文件行#1:错误夸大类

Error is throws in the following line in MainActivity.cs 's OnCreate() method : MainActivity.csOnCreate()方法的以下行中引发错误:

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. 但是,如果我将背景设置为ic_launcher ,它看起来ic_launcher徽标,但是作为.xml文件存储在mipmap-anydpi-v26文件夹中,则可以完美地工作。 The ic_launcher.xml looks like this: ic_launcher.xml如下所示:

<?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. 我已经看过其他SO帖子,但是几乎所有这些帖子似乎都包含drawable文件夹,在我的情况下是缺少的。 Even in a new project, the folder is missing. 即使在新项目中,该文件夹也丢失。 You can simply reproduce it by creating a blank Xamarin.Android project. 您可以通过创建一个空白Xamarin.Android项目来简单地重现它。

So, any ideas on why it may be crashing on on a physical device? 那么,关于它为何可能在物理设备上崩溃的任何想法?

...have the drawable folder which is missing in my case. ...在我的情况下缺少drawable文件夹。

Just create a drawable folder within your Resources folder. 只需在Resources文件夹中创建一个drawable文件夹。

Note: Do this via the IDE's solution/project explorer, not the file-system. 注意:通过IDE的解决方案/项目资源管理器而不是文件系统来执行此操作。

There are a number of Resources folders and Xamarin templates do not create all of them by default. 有许多“资源”文件夹,并且Xamarin模板默认情况下不会创建所有文件夹。

Note: They actually become res/xxxx within the Android APK file structure. 注意:它们实际上成为Android APK文件结构中的res/xxxx

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

在此处输入图片说明

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

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