Aousaf Rashid
Aousaf Rashid

Reputation: 5758

Xamarin.Android Crashes When Trying To Load Style From Resource

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?

Upvotes: 0

Views: 324

Answers (1)

SushiHangover
SushiHangover

Reputation: 74209

...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

enter image description here

Upvotes: 1

Related Questions