question

RodneyAnonymous-4478 avatar image
0 Votes"
RodneyAnonymous-4478 asked JarvanZhang-MSFT commented

Xamarin Forms: DisplayAlert calls with android:Theme.Material style throws exception: "Android.Content.Res.Resources+NotFoundException: Resource ID #0x0"

I am working on a mobile app using Xamarin Forms. To achieve the look I want, I have derived my theme from android:Theme.Material, as follows in the styles.xml file:

   <style name="MainTheme" parent="android:Theme.Material">
       ...
   </style>

This results in any call to the DisplayAlert method in the shared library throwing the following exception:

 Android.Content.Res.Resources+NotFoundException: Resource ID #0x0

I can reproduce this error reliably in a completely new project just by changing the theme parent to android:Theme.Material in styles.xml.

I have no idea how to solve this problem and have not been able to find anyone else who has had or solved this issue by searching. The exception suggests that there is a resource I need to add somewhere, but I have no idea what or to where.

Full stack trace below:

 Android.Content.Res.Resources+NotFoundException: Resource ID #0x0
   at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <26521a5118b44c858c385715922b9d5d>:0 
   at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <26521a5118b44c858c385715922b9d5d>:0 
   at Android.App.Dialog.Show () [0x0000a] in <d3b924763d4a465c85b26f6e8edc8a53>:0 
   at Xamarin.Forms.Platform.Android.PopupManager+PopupRequestHelper+FlexibleAlertDialog.Show () [0x00008] in D:\a\1\s\Xamarin.Forms.Platform.Android\PopupManager.cs:496 
   at Xamarin.Forms.Platform.Android.PopupManager+PopupRequestHelper.OnAlertRequested (Xamarin.Forms.Page sender, Xamarin.Forms.Internals.AlertArguments arguments) [0x00111] in D:\a\1\s\Xamarin.Forms.Platform.Android\PopupManager.cs:152 
   at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
   at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 
   --- End of managed Android.Content.Res.Resources+NotFoundException stack trace ---
 android.content.res.Resources$NotFoundException: Resource ID #0x0
     at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:237)
     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2281)
     at android.content.res.Resources.getLayout(Resources.java:1175)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
     at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
     at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
     at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
     at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
     at android.app.Dialog.dispatchOnCreate(Dialog.java:421)
     at android.app.Dialog.show(Dialog.java:315)
     at mono.java.lang.RunnableImplementor.n_run(Native Method)
     at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
     at android.os.Handler.handleCallback(Handler.java:883)
     at android.os.Handler.dispatchMessage(Handler.java:100)
     at android.os.Looper.loop(Looper.java:214)
     at android.app.ActivityThread.main(ActivityThread.java:7356)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 }    Android.Content.Res.Resources.NotFoundException
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

can reproduce this error reliably in a completely new project just by changing the theme parent to android:Theme.Material in styles.xml.

I created a new project and consume the 'android:Theme.Material' theme, the project will raise an error while building. The VS on my pc is lastest version 16.10. If I create a Xamarin.Android project and change the MainActivity's type from AppCompatActivity to Activity, it'll work fine. Theme.Material only works for the Activity class which doesn't have action bar. AppCompatActivity has already provides Material color themes, check the doc. https://developer.android.com/topic/libraries/support-library/features#app-components

To achieve the look I want

The theme of Xamarin.Forms Android platform is different than a Xamarin.Android project. If you want to change the color of the theme in Xamarin.Forms, try using the following properties that have been provided in the styles.xml.

<style name="MainTheme" parent="MainTheme.Base">

  <!-- colorPrimary is used for the default action bar background -->
  <item name="colorPrimary">#2196F3</item>

  <!-- colorPrimaryDark is used for the status bar -->
  <item name="colorPrimaryDark">#1976D2</item>

  <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
  <item name="colorAccent">#FF4081</item>
</style>
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am not asking how to change style or colors. I am not having any problem with android:Theme.Material working properly other than this specific exception being thrown when calling DisplayAlert.

If I create a Xamarin.Android project and change the MainActivity's type from AppCompatActivity to Activity, it'll work fine.

Creating a new Xamarin.Forms project (NOT just Xamarin.Android) and changing the MainActivity in the Android project from global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity (the default) to Activity breaks the code and the app, as the default LoadApplication called in OnCreate is no longer a valid method.

Please re-read my question - it is about an exception, not how to change themes. It is about Xamarin Forms, not Xamarin.Android.

0 Votes 0 ·

Sorry for the mistake, I am just trying to analyze the cause to the issue. As you said, you changed the FormsAppCompatActivity to Activity to consume the 'android:Theme.Material', but that make the LoadApplication method is not valid. The LoadApplication is used to instantiate the App class in each platform-specific project to start the shared project. How do you start the shared project and display the contentPages without using the method?

0 Votes 0 ·

As you said, you changed the FormsAppCompatActivity to Activity to consume the 'android:Theme.Material'

I didn't say that at all. You suggested making that change would resolve my issue. It does not, because making that change breaks the call to LoadApplication and the code no longer compiles.

How do you start the shared project and display the contentPages without using the method?

I don't. No change was necessary to my MainActivity in order to use android:Theme.Material. I changed styles.xml. That was all I did. I said that in my original post, which I will quote below:

I can reproduce this error reliably in a completely new project just by changing the theme parent to android:Theme.Material in styles.xml.

I feel I explained my problem very clearly, but your continued misunderstanding is taking up my time in having to repeatedly explain it to you. Please understand my problem properly before continuing to try to help me with it.

0 Votes 0 ·
Show more comments