I want to play a mp4 video which I have put in the folder Resources/raw. The following is my code and the error "A field initialiser can not be applied to the non static fiel or non static method or property" (translated from German) is displayed, 'FindViewById(Resource.Id.vv)' being the underlined part. In my layout file I have included a VideoView with the id 'vd'. I would be very thankful for every help to play my video!
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
namespace App_1_design
{
[Activity(Theme = "#style/AppTheme", MainLauncher = true]
public class MainActivity : AppCompatActivity
{
VideoView vid = (VideoView) FindViewById(Resource.Id.vv);
We need to initialize controls in the OnCreate event after setting the layout file like:
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
VideoView vid;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
vid = FindViewById<VideoView>(Resource.Id.vv);
var uri = Android.Net.Uri.Parse("android.resource://" + PackageName + "/" + Resource.Raw.sample_video);
vid.SetVideoURI(uri);
vid.Start();
}
}
Update
why now the error message says 'resource does not contain a definition
for raw'?
Make sure the build action of your video is AndroidResource. And then we could access this resource through Resource.Raw.[yourVideoID].
Related
ConnectivityManager connMgr = (ConnectivityManager)this.Activity.GetSystemService(Android.Content.Context.ConnectivityService);
NetworkInfo networkInfo = connMgr.ActiveNetworkInfo;
bool isConnected = networkInfo.IsConnected;
this code is in the onCreateView method in my fragment and when i run the app i get the error:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object. occurred
it would be great if someone would explain to me what "not set to an instance of an object means" as i dont quite understand. still a little new to xamarin and c# in general.
heres the fragament1.cs file:
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Webkit;
namespace TabsApp.Fragments
{
public class Fragment1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment1 NewInstance()
{
var frag1 = new Fragment1 { Arguments = new Bundle() };
return frag1;
}
WebView webView;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
View v = inflater.Inflate(Resource.Layout.fragment1, container, false);
webView = v.FindViewById<WebView>(Resource.Id.webView);
webView.LoadUrl("https://google.com");
webView.Settings.JavaScriptEnabled = true;
webView.SetWebViewClient(new WebViewClient());
return v;
}
public void OnBackPressed()
{
webView.GoBack();
}
}
}
and heres the MainActivity.cs file:
using Android.App;
using Android.OS;
using TabsApp.Fragments;
using Android.Content.PM;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
namespace TabsApp
{
[Activity(Label = "#string/app_name", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon = "#drawable/icon",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : AppCompatActivity
{
BottomNavigationView bottomNavigation;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
SupportActionBar.SetHomeButtonEnabled(false);
}
bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavigation.NavigationItemSelected += BottomNavigation_NavigationItemSelected;
LoadFragment(Resource.Id.menu_home);
}
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
void LoadFragment(int id)
{
Android.Support.V4.App.Fragment fragment = null;
switch (id)
{
case Resource.Id.menu_home:
fragment = Fragment1.NewInstance();
break;
case Resource.Id.menu_audio:
fragment = Fragment2.NewInstance();
break;
case Resource.Id.menu_video:
fragment = Fragment3.NewInstance();
break;
}
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
}
}
Well its pretty simple this looks to me like you do not have your activity context here and for that reason you are unable to get the connMgr
What you need to do is when you call your fragment from your activity pass the current activity to your ~Fragment~ something like this on your fragment call:
Fragment1 newInstance(this);
Receive it in your fragment with something like this in your constructor:
public static AppCompatActivity _activity
public static Fragment1 newInstance(AppCompatActivity activity )
{
_activity =activity ;
//Lines of code }
And in your onCreateView use it like this :
ConnectivityManager connMgr = (ConnectivityManager)_activity.GetSystemService(Android.Content.Context.ConnectivityService);
NetworkInfo networkInfo = connMgr.ActiveNetworkInfo;
bool isConnected = networkInfo.IsConnected;
If it doesn't work lemme know! Goodluck
I'm novice in programming and this is my first project ever in c# xamarin. I want to set loop on that MediaPlayer but SetLooping method is missing in Android.Media.
I'm using Visual Studio 2017 Community. Here is my code.
Thanks in advance for any feedback :D
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Media;
using Android.Content;
using Android.Preferences;
using Android.Views.Animations;
namespace App2
{
[Activity(Label = "FirstApp 1.34", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
MediaPlayer music;
protected override void OnPause()
{
base.OnPause();
music.Pause();
}
protected override void OnDestroy()
{
music.Release();
base.OnDestroy();
}
protected override void OnResume()
{
music.Start();
base.OnResume();
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
music = MediaPlayer.Create(this, Resource.Raw.bgsound);
music.SetVolume(0.7f, 0.7f);
music.Start();
}
SetLooping method is mapped as a property in Xamarin.Android and you should use the Looping property instead. Take a look at: Android.Media.MediaPlayer.Looping Property
Try this on your code
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
music = MediaPlayer.Create(this, Resource.Raw.bgsound);
music.SetVolume(0.7f, 0.7f);
music.Start();
music.Looping = true;
}
My Splash Activity just shows a plain white screen. I am in a Xamarin Forms project. When I tried using a style to set the background, it worked, but the image was stretched out, which is why I am trying to use a LinearLayout and ImageView instead. This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Buddy.Droid
{
[Activity(Label = "HeyBuddy", Icon = "#drawable/icon", Theme = "#style/splashscreen", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
var imageView = new ImageView(this);
imageView.SetImageResource(Resource.Drawable.splash_screen);
var layout = new LinearLayout(this);
layout.SetBackgroundColor(Color.ParseColor("#0098CC"));
layout.AddView(imageView);
SetContentView(layout);
}
protected override void OnResume()
{
base.OnResume();
StartActivity(typeof(MainActivity));
}
}
}
To programmatically create a Layout with an ImageView:
Create a ViewGroup.LayoutParams with its width and height set to MatchParent
Assign the layoutparams to your ImageView
Assign a ImageView.SetScaleType for how you want to scale your drawable (i.e. ImageView.ScaleType.FitCenter)
Note: You do not need the LinearLayout, just set the background color of your ImageView
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
var imageView = new ImageView(this)
{
LayoutParameters = layoutParams
};
imageView.SetImageResource(Resource.Drawable.splash_screen);
imageView.SetScaleType(ImageView.ScaleType.FitCenter);
imageView.SetBackgroundColor(Color.ParseColor("#0098CC"));
SetContentView(imageView);
}
protected async override void OnResume()
{
base.OnResume();
await Task.Delay(5000); // simulate some background work....
StartActivity(typeof(MainActivity));
}
I wanna point out that i'm a total newbie in Xamarin.
My goal is to create simple app that after you push the button, it will jump to another layout. My problem is, that after i created a new layout(called layout1) and new activity, I can't reference to this layout in my MainActivity.
freshly created Activity:
namespace Layouts
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
}
}
}
And my MainActivity:
namespace Layouts
{
[Activity(Label = "Layouts", MainLauncher = true, Icon = "#drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView (Resource.Layout.Main);
Button button1 = FindViewById<Button>(Resource.Id.button);
button1.Click += delegate
{
StartActivity(typeof(layout1)); //can't reference layout1 there
};
}
}
}
Any help will be great, thanks :)
StartActivity(typeof(Activity1));
I am building a Xamarin Android app using Xamarin.Forms my App class is really simple and contains one page like so:
public App()
{
// The root page of your application
MainPage = new NavigationPage(new ContentPage()
{
Title = "My First Page!",
Content = new Label()
{
Text = "Test Page!",
TextColor = Color.White,
FontSize = 25,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
}
});
}
and my MainActivity class like so:
[Activity(Label = "GridTest", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
When I launch the application though the NavigationBar is displayed with the word "GridTest" displayed in the top left which looks rubbish, shown below:
This is the NavigationBar I would like to hide.
After this my application loads with the navigation bar fine. Shown below:
I would like to keep this Navigation Bar
So my question is. How do I hide the Android NavigationBar that is displayed BEFORE the initial page has loaded?
SIDE NOTE:
I have tried NavigationPage.SetHasNavigationBar(page,false); but this does not work. This hides the NavigationBar for the first page shown below.
which I would like to keep! This also does not hide the "GridTest" navigation bar I am looking to hide (shown in screenshot 1)
To reiterate... I am looking to hide the INITIAL navigation bar. provided by the Android MainActivity NOT the Xamarin.Forms navigation bar
The main activity which will load the first time the application starts.
[Activity(Label = "testapp", MainLauncher = true, Theme = "#style/MyTheme", ScreenOrientation = ScreenOrientation.Portrait)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// SetContentView(Resource.Layout.LaunchScreen); //Set a splash screen here if you wish
Java.Lang.Runnable runnable = new Java.Lang.Runnable(() =>
{
Intent i = new Intent(this, typeof(MainActivity));
StartActivity(i);
});
new Handler().PostDelayed(runnable, 1000);
}
}
The theme for that activity must be saved to Resources/drawable/styles.xml and is
<resources>
<style name="MyTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
This SplashActivity will load and call MainActivity
and your MainActivity will look like this:
[Activity(Label = "GridTest", Icon = "#drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
You dont need to set the Theme here and it will work as before. Another Important note about the MainActivity is you MUST remove MainLauncher = true
You should be able to hide that navigation bar by setting a simple attribute in your page like so:
<ContentPage NavigationPage.HasNavigationBar="false"
..... >
</ContentPage>