'Layout not displaying content when loaded through new activity in Xamarin
I'm developing an app with Xamarin.
I have three activities, DiallerActivity, ContactsActivity and SplashActivity - and two .axml layouts, Main.axml and Contacts.axml
The SplashActivity is the first loaded which displays a splashscreen on opening the app, when it's done it loads the DiallerActivity which displays my Main.axml layout - this works fine.
Inside my Main.axml layout I have a button which when clicked loads the ContactsActivity which should then load the Contacts.axml which just has 3 buttons inside and a label.. none of which are programmed to do anything.
The problem is that when the button is clicked, the display changes to a blank screen, still showing the android bar at the top of the screen.. it just does not show any content from the .axml file.
I need the Contacts.axml layout to be displayed when the activity is run.. I hope i've made this clear. My current code is below.
Code for DiallerActivity
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
Button btnAcceptClick = FindViewById<Button> (Resource.Id.btnAccept);
btnAcceptClick.Click += delegate {
StartActivity (typeof(VoWiFiApplicationFinal.ContactsActivity));
};
Code for ContactsActivity
public class ContactsActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// setting the contacts.axml as the view
SetContentView (Resource.Layout.Contacts);
}
}
Does anyone have any idea why the Contacts.axml isn't displayed? If you need me to provide any more information just say and i'll bring it over.. I am using C# as my language by the way, so i'd prefer help related to that if it even applies to the question in mind. Thanks for reading.
Contacts.xaml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/toptest"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+/label1"
android:text="testlabel" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/testagain"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="fill_parent"
android:id="@+id/menuBar"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:text="ACCEPT"
android:id="@+id/btnAccep"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btnDeclin"
android:layout_weight="1"
android:text="DECLINE" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/btntes"
android:layout_weight="1"
android:text="TEST" />
</LinearLayout>
</LinearLayout>
Solution 1:[1]
There seems to be a bug in Xamarin somewhere. I also got a blank screen (except for the action bar) when I renamed the .axml-file back and forth and experimented with strings in values\strings.xml, sometimes giving them the same name as the .axml-file. Not sure if the strings had anything to do with it though, but I remember getting some kind of error when compiling at one point.
Anyway, the solution for me was to
- Backup the contents of the .axml-file and delete the file completely.
- Create a new .axml-file with another name than the original.
- Paste the backed up content into the new file.
- Rename the new file back to the old name.
After that I did a test run and everything worked again.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mingo |
