Following this tutorial, after I created the project, there is already an error, and I haven't done anything yet:
The name 'Resource' does not exist in the current context
using Android.App;
using Android.Widget;
using Android.OS;
namespace Phoneword
{
[Activity(Label = "Phoneword", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main); <-- Error here
}
}
}
Tried the answers here, but didn't work.
Checked this link, but as I said, I haven't done or add anything, so it does not work for me
Tried Clean, Rebuild, Build, Restart but still the same
Tried changing Build Action to None. Although the error disappeared, a new one appeared when debugging
The weird thing is I can start debugging even if there is an error. Sometimes it is okay, but after a minute it will produce the red underline, but the word Resource has a color
What would be the way/setup to completely remove this error?
Related
I am trying to implement an splash screen for the Android part of my UNO solution. I can get the splash screen to appear, wait a few seconds but upon navigation to the main page I get the following exception in the app.cs
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
// this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
Frame rootFrame = Windows.UI.Xaml.Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame(); <<<<< exception
Unhandled Exception:
Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference occurred
The stack trace is as simple as :
0x25 in Uno1.App.OnLaunched at C:\Users\pjsta\Documents\Visual Studio 2017\Projects\Uno1\Uno1\Uno1.Shared\App.xaml.cs:55,17 C#
The relevant part of the solution are
1. my new SplashActivity in the Android project
[Activity(Label = "SplashScreen", MainLauncher = true, Theme = "#style/Theme.SplashActivity")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
System.Threading.Thread.Sleep(1000);
StartActivity(typeof(MainActivity));
}
}
Modification to MainActivity to not be the MainLauncher
[Activity(
MainLauncher = false,
ConfigurationChanges = ConfigChanges.Orientation |
ConfigChanges.ScreenSize,
WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden )]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity
{
}
The relevant style loads fo the splash screen OK. Switching the MainActivity back to MainLauncher=true works , albeit without a splash screen.
I am a newbie to Xamarin and Android development, but competent in UWP. Anyone out there got any ideas?
From the exception, it sounds like when new Frame() is called, the base native constructor is called with a null Context. This is probably because Uno expects ApplicationActivity to be run as MainLauncher=true. It's possible that inheriting your SplashActivity class from Uno.UI.BaseActivity could resolve the error.
A better way to show a splash screen on Android is to modify the theme rather than create a separate activity. I'll use the Uno Gallery app as an example.
Create a file in the Resources/drawable folder of your Android head called, eg, splash.xml. Define your splash screen visual appearance here.
Open the Resources/values/Styles.xml file. Inside the 'AppTheme' style add the following line:
<item name="android:windowBackground">#drawable/splash</item>
Hope that helps. Also, please tag questions about the Uno Platform as 'uno-platform' (the 'uno' tag refers to the OpenOffice component model).
This is my first Xamarin app. I have my environment set up in Visual Studio, and have a template project which I can run in the Android emulator.
Having dragged and dropped a couple of controls on to the designer surface, I find that when I run the application in the emulator, neither of the two controls that I have added (a button and a switch) display in the emulator.
I have tried:
Cleaning the solution
Rebuilding the solution
Manually deleting bin and object files
Unchecking 'Use Fast Deployment' in project Android Options
Uninstalling from the emulator
But with the same result each time.
[Project files removed]
Am I missing something?
Just downloaded your code and ran it. Uncommenting SetContentView (Resource.Layout.Main); it worked to me.
[Activity(Label = "Tgo.AndroidApp", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
}
}
Here a nice guide to get you started with Xamarin Android.
navigate to MainActivity.cs and uncomment this code
SetContentView (Resource.Layout.Main);
and rebuild the solution and debug.
Let me know it if helps!
In my case, I had to update my Main.xml file with the updates I put into the Main.axml and Rebuild.
I added a new layout file to my android project, I tried to reference the layout in my activity class like this SetContentView(Resource.Layout.newlayout), when I built my project, I got newlayout doesn't exist.
I got this fixed by changing the new layout build action from "BundleResource" to "AndroidResource" in the layout properties tab.
You need to simply build the solution once again to get the Layout.
Xamarin android will convert the resource files into int during build.
lemme know if you find any errors.
[Activity(Label = "Hello", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait, Theme = "#android:style/Theme.Black.NoTitleBar")]
public class NewActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.newlayout);
}
}
newLayout.axml
Rebuild the solution and it should work.
Post errors too if not.
In Xamarin, I have pasted the following code from a working activity into a blank new activity:
[Activity (Label = "SimpleOnePageViewPager", MainLauncher = true)]
public class MainActivity : FragmentActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
FragmentItem fragItem = new FragmentItem ();
List<Android.Support.V4.App.Fragment> fragments = new List<Android.Support.V4.App.Fragment>();
fragments.Add(fragItem);
ViewPagerAdapter pageAdapter = new ViewPagerAdapter(SupportFragmentManager, fragments);
var pager = FindViewById<ViewPager>(Resource.Id.MainViewPager);
pager.Adapter = pageAdapter;
}
}
This is the error that I am getting?
Error CS0103: The name 'Resource' does not exist in the current context
Can I please have some help with this code?
Thanks in advance
Make sure you added namespace using Project_Name;
I had this problem too, and solved with this steps:
Open MainActivity.cs of the Android project and put the line:
using AppName.Droid
This line is the name of namespace in the file Resource.designer.cs
You can change the MainActivity.cs Build Action property as 'none' instead of 'compile'. I Hope this will resolve your problem.
Thanks,
Imman
It was exactly the problem that I just solved, I think that this is a Xamarin Bug, when you add an existing xaml file it add it to the project without set it as embedded resource as it should be, and without set the custom tool command that must be set to: MSBuild:UpdateDesignTimeXaml
I met same problem today. I searched around, and also tried above solutions, but it did not work from my side.
I already resolved the problem by myself with bellow steps:
Update xamarin nuget packages
Clean and rebuild the project
P/S: hope it can help others if they meet same problem.
Happy coding!
In some case, when accessing an activity field from an anonymous class, I get NoSuchFieldError at runtime: java.lang.NoSuchFieldError: MyActivity.myField
EDIT: I'm now sure that it's an Android issue, because everything compiles correctly but resolved incorrectly at runtime. When changing to MyActivity.this.myField instead of plain myField, everything works perfect.
BTW, I rechecked and I don't have any other myField anywhere else, Also, when opening declaration in Eclipse, it goes to the intended field (myField).
The only problem is that I couldn't reproduce this in a sample project. I'm left only to state that it's a specific problem in my project, but I'm reluctant doing so and I can't publish this project.
I'll keep this open in case someone stumbles upon something similar.
Here's the code that I can publish that crashes my project but not the sample one:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Runnable() {
#Override
public void run() {
Toast.makeText(MyActivity.this, myField.getName(), Toast.LENGTH_SHORT).show();
}
}.run();
}
It seems that accessing the field with the class' prefix solves it: MyActivity.this.myField
Strange. I guess it's something with Android's field resolution in runtime.