I have a Xamarin.Forms project. Here is my MainActivity.cs from the Android part of the project:
[Activity(Label = "MyApp.Droid", Icon = "#drawable/icon", Theme = "#style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
Every time I try to test on my device I get an Android.Util.AndroidRuntimeException with the following message: requestFeature() must be called before adding content even though I'm not calling requestFeature() at all. Here's the stack trace:
--- End of managed Android.Util.AndroidRuntimeException stack trace ---
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:338)
at md5f4656b392f6d15b90a32ccb2cb36af91.MainActivity.n_onCreate(Native Method)
at md5f4656b392f6d15b90a32ccb2cb36af91.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6672)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)
I've already tried restarting Xamarin Studio and cleaning/rebuilding my solution. What am I missing?
Edit: Just created a new blank Xamarin.Forms project. It ran perfectly on my device. Still can't get the old project to run though.
Solution:
Close project
Close Xamarin Studio/Visual Studio
Delete app (along with app data) from phone/emulator
Go to project directory, delete bin and obj folders
Open Xamarin Studio/Visual Studio
Re-deploy
Issue should be resolved now
Related
I am using Visual Studio for Mac 7.6.6 to create a Xamarin.Forms app targeting macOS (to be shared with something running on Windows). I create a new Project and select
Multiplatform App | Blank Forms App. Click Next
Configure your Blank Forms App. There are target platforms: Android and iOS.
(There is nothing for macOS). Since I have not installed the build toolkits for either iOS and android, both of these checkboxes are disabled. Therefore the Next button on this wizard page is disabled.
How do I proceed? I assume there is no way to use the New Project wizard for this.
I came across an old post for starting with a Xamarin Cocoa app and using NuGet to put the Xamarin Forms functionality but don't understand the code
LoadApplication(new App()); // <-- don't know what App is
I suspect the VS Mac and Xamarin.Forms are out of sync being on the bleeding edge. Has anyone gotten this to work?
I would suggest following SushiHangover's suggestion since that is simpler which is what you have done already:
Add a new CocoaApp project to your solution.
Install Xamarin.Forms NuGet package into your CocoaApp.
Reference the shared project or .NET Standard project from your CocoaApp project.
Edit info.plist and remove the source entry (NSMainStoryboardFile).
Change the AppDelegate to derive from Xamarin.Forms.Platform.MacOS.FormsApplicationDelegate.
Update Main.cs to initialize the AppDelegate
In the AppDelegate's DidFinishLaunching add the code to initialize Xamarin.Forms.
Create a new NSWindow which should be returned from the MainWindow property in the AppDelegate.
Main.cs:
static class MainClass
{
static void Main(string[] args)
{
NSApplication.Init();
NSApplication.SharedApplication.Delegate = new AppDelegate();
NSApplication.Main(args);
}
}
AppDelegate.cs:
[Register("AppDelegate")]
public class AppDelegate : Xamarin.Forms.Platform.MacOS.FormsApplicationDelegate
{
NSWindow window;
public AppDelegate()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
window.Title = "Xamarin.Forms on Mac!";
window.TitleVisibility = NSWindowTitleVisibility.Hidden;
}
public override NSWindow MainWindow
{
get { return window; }
}
public override void DidFinishLaunching(NSNotification notification)
{
Xamarin.Forms.Forms.Init();
LoadApplication(new App());
base.DidFinishLaunching(notification);
}
public override void WillTerminate(NSNotification notification)
{
// Insert code here to tear down your application
}
}
However Visual Studio for Mac does include a Mac project with the Xamarin.Forms project templates. However it does not expose this in the New Project dialog currently. You can create a Mac Forms project from this template but it is a bit more work than what SushiHangover suggested and you have used.
Install the Xamarin.Forms project template into the .NET Core project templates
dotnet new --install "/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/AddIns/Xamarin.Forms.Addin/Templates/Xamarin.Templates.Multiplatform.0.0.1.nupkg"
Create a new Forms project including the Mac project (you may want to review and set other template parameters - the following creates a blank Forms app with Android, iOS, Mac, UWP and a Shared project).
dotnet new forms-app --CreateMacProject -k blank
Create a new blank solution (Other - Miscellaneous - Blank Solution) in the parent directory of the projects you just created.
Add all those projects created to the solution.
Then you can build and run the Mac project which includes Xamarin.Forms.
Note you may want to remove the Xamarin.Forms project template from the .NET Core project templates which you can do by running the following:
dotnet new --debug:reinit
In addition to the approved answer, which is incomplete with recent updates, now you have to do one more step.
Link the delagate in the Mac project main class
main.cs:
static class MainClass
{
static void Main(string[] args)
{
NSApplication.Init();
NSApplication.SharedApplication.Delegate = new AppDelegate();
NSApplication.Main(args);
}
}
nota. My edit was refused and I'm not allowed to add comments. So I added a complementary answer to help those looking for help now.
Recently I asked a question over trying to get my theme recognized within the android test project: Unit testing a Xamarin Forms Android specific code project
I'm not running into a conflict that shows up within xunit / nunit as the testing strategy. I feel like it's because the FormsAppCompatActivity with an older activity type, but I'm new to Xamarin and am unsure on how to approach this.
I get a lot of these type of errors:
Attribute "layout_anchorGravity" already defined with incompatible format.
The full list of the similar Attribute errors is:
fabSize
tabMode
expandedTitleGravity
layout_scollFlags
layout_collapseMode
collapsedTitleGravity
tabGravity
showDividers
displayOptions
showAsActions
actionBarSize
finally I have the addition error:
Found tag styles where item is expected
//Within the Android Forms project, I have this inheritance:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
// typical code
}
//within the Android UITest project
public class MainActivity : Xunit.Runners.UI.RunnerActivity {
// tests can be inside the main assembly
AddTestAssembly(Assembly.GetExecutingAssembly());
AddExecutionAssembly(typeof(ExtensibilityPointFactory).Assembly);
// or in any reference assemblies
//AddTestAssembly(typeof(PortableTests).Assembly);
// or in any assembly that you load (since JIT is available)
#if false
// you can use the default or set your own custom writer (e.g. save to web site and tweet it ;-)
Writer = new TcpTextWriter ("10.0.1.2", 16384);
// start running the test suites as soon as the application is loaded
AutoStart = true;
// crash the application (to ensure it's ended) and return to springboard
TerminateAfterExecution = true;
#endif
// you cannot add more assemblies once calling base
base.OnCreate(bundle);
}
Hi everyones i got a anoying problem:
The application could not be started. Ensure that the application has been installed to the target device and has a launchable activity (MainLauncher = true).
Things i do:
adb uninstall ; nothing happens
try to uninstal app from device: the app dont apeard, even like a fake app whith 0kb
Uninstall Mono Shared Runtine and Api from app
Factory reset
Check in another device
Nothing solve my problem and i dont know who i must do.
Add:
My MainActivity;
[Activity(Label = "AppTecnicos", Icon = "#drawable/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
I found the error (Finally): my app package name started with uppercase.
AppTecnicos.Droid
I changed it to:
appTecnicos.Droid
The application could not be started. Ensure...has a launchable activity (MainLauncher = true).
One Activity in your application needs to be assigned as the MainLauncher otherwise the Android OS does not know the main entry point to app and what to display.
In Xamarin.Android typically this is done via the ActivityAttribute by assigning MainLauncher = true.
Example:
[Activity(Label = "StackOverflow", MainLauncher = true, Icon = "#mipmap/icon")]
public class MainActivity : AppCompatActivity
{
~~~
}
Ref: ActivityAttribute Class
If you are using visual studio and your aplication is a Xamarin.Forms aplication, this could be happening due to a solution configuration problem.
In that case, try to open the solution properties and within the configuration properties section check that your proyect its checked for deploy.
I have a suite of UITest for our Xamarin Forms project. All of the AutomationId's are present in the XAML file and work fine on the iOS project. However when running the tests on Android the AutomationId is not there which is causing my tests to fail.
I've doubled checked using REPL and tree to inspect each view for it's Id's
Are you initializing the AutomationId's in the OnCreate of the MainActivity in the Android native project?
After the Xamarin.Forms.Forms.Init (this, bundle); :
Xamarin.Forms.Forms.ViewInitialized += (object sender, Xamarin.Forms.ViewInitializedEventArgs e) => {
if (!string.IsNullOrWhiteSpace (e.View.AutomationId)) {
e.NativeView.ContentDescription = e.View.AutomationId;
}
};
In android app build using Xamarin Forms, the automation id is converted to cont-desc. You can use cont-desc for your test.
There is no automationId itself visible in REPL, but only label or id with the value equal to that set on the automationId, so you should just use Marked passing in the value.
I am creating a Xamarin Forms Project and using android.support.v7.widget.Toolbar with FormsAppCompatActivity. The code I'm posting below was working with Xamarin.Android.Support.v7.AppCompat Version: 23.0.1.3
But after I upgraded to 23.3.0 this has started throwing Android.Views.InflateException: Binary XML file line #1: Error inflating class android.support.v7.widget.toolbar exception.
Here is my code for MainActivity
[Activity(Label = "AppMob.Client", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
FormsAppCompatActivity.ToolbarResource = Resource.Layout.toolbar;
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Xamarin.FormsMaps.Init(this,bundle);
GoogleAnalyticsHelper.GetGASInstance().Initialize_NativeGAS(Application.Context); //For GA initialization
LoadApplication(new App());
}
}
This is my code for Resource.Layout.toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways" />
I have updated Xamarin Forms to 2.2.0.31
Dependencies for this version of Xamarin Forms mention:
Xamarin.Android.Support.v4 (= 23.3.0)
Xamarin.Android.Support.Design (= 23.3.0)
Xamarin.Android.Support.v7.AppCompat (= 23.3.0)
Xamarin.Android.Support.v7.CardView (= 23.3.0)
Xamarin.Android.Support.v7.MediaRouter (= 23.3.0)
it should thus be compatible with 23.3.
May you don't declare theme of layout in your activity.cs
you must add this to youractivity.cs:
Theme = "#style/MyThemesss"
Once I had your problem and I did the below steps to solve it :
Call RequestWindowFeature(WindowFeature.Notiltle) in the first line of OnCreate(Bundle bundle) and above everything else and double check if you did remove the ActionBar .
Clean solution , Remove all support library dependencies , change toolbar Id and restart Visual Studio (or Xamarin Studio) Then re-add support libraries and build solution. (sometimes the cause of error is caching or big piles of other junk files in solution .)
Consider not using "?attr/colorPrimary" instead call the color resource directly.
and after all if you could post your style xml file and main layout ,i would helped more efficiently.
This error is resolved as follows.
You need to update Xamarin Forms for that solution. In the NuGet Package.
The package that says CardView too.
Then you should do clean, rebuild.
This will solve this problem.
I had same problem,
probably you may delete some option in tag like "app:layout_scrollFlags" that not supported in api 21, if all options are ok, delete "?attr" values and put their real value instead.
In my case, I found that I had written android.suport.v7.widget.Toolbar ("suport" with one letter "p" instead of "support")
And it worked immediately when I fixed it.
Cleaning solution and restarting VS helped me.
When debugging Xamarin project, go to Debug->Windows->Exception Settings
Under Common Language Runtime Exceptions uncheck System.Exception