Toast通知不起作用

本文关键字:不起作用 通知 Toast | 更新日期: 2023-09-27 18:13:23

我写了一些代码,检查网络连接的可用性,但它不起作用。

我没有看到任何toast通知。我在manifest文件中写了所有的权限。

这段代码有什么问题?

namespace MurakamiKiev
[Activity(Label = "Murakami",MainLauncher = true,Icon = "@drawable/logo", Theme = "@android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
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);
        ImageButton next = FindViewById<ImageButton> (Resource.Id.nextButton);
        ImageButton previous = FindViewById<ImageButton> (Resource.Id.previousButton);
        ImageButton home = FindViewById<ImageButton> (Resource.Id.homeButton);
        ImageButton cart = FindViewById<ImageButton> (Resource.Id.cartButton);
        Button sushi = FindViewById<Button> (Resource.Id.sushiButton);
        Button sets = FindViewById<Button> (Resource.Id.setsbutton);
        //Button rolli = FindViewById<Button>(Resource.Id.rollibutton);
        ImageButton menu = FindViewById<ImageButton> (Resource.Id.menuButton);
        //Otslezivaem click po knopke 'next' i perehodim dalshe
        /*rolli.Click += delegate
        {
            var intent139 = new Intent(this, typeof(RolliActivity));
            StartActivity(intent139);
        };*/
        sets.Click += delegate {
            var intent121 = new Intent (this, typeof(SetsActivity));
            StartActivity (intent121);
        };
        sushi.Click += delegate {
            var intent24 = new Intent (this, typeof(SushiActivity));
            StartActivity (intent24);
        };
        next.Click += delegate {
            var intent = new Intent (this, typeof(MenuActivity));
            StartActivity (intent);
        };
        //Otlezivaem click po knopke 'Korzina' i perehodim v nee
        cart.Click += delegate {
            var intent2 = new Intent (this, typeof(CartActivity));
            StartActivity (intent2);
        };
        menu.Click += delegate {
            var intent39 = new Intent (this, typeof(MenuTopActivity));
            StartActivity (intent39);
        };
    }
    public void CheckNetwork()
    {
        var connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
        var activeConnection = connectivityManager.ActiveNetworkInfo;
        if ((activeConnection != null) && activeConnection.IsConnected)
        {
            Toast.MakeText (this, "OK", ToastLength.Short).Show ();
        } 
        else 
        {
            Toast.MakeText (this, "Connect to the wi-fi", ToastLength.Short).Show ();
        }       
    }
}

Toast通知不起作用

这段代码有什么问题?

你没有在任何地方调用你的方法,所以它永远不会显示你的Toast,因为方法是从来没有调用。

如何解决?

例如,如果您想在启动Activity时检查连接,只需添加如下方法:

 protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    // Set our view from the "main" layout resource
    SetContentView (Resource.Layout.Main);
    ImageButton next = FindViewById<ImageButton> (Resource.Id.nextButton);
    ImageButton previous = FindViewById<ImageButton> (Resource.Id.previousButton);
    ImageButton home = FindViewById<ImageButton> (Resource.Id.homeButton);
    ImageButton cart = FindViewById<ImageButton> (Resource.Id.cartButton);
    Button sushi = FindViewById<Button> (Resource.Id.sushiButton);
    Button sets = FindViewById<Button> (Resource.Id.setsbutton);
    //Button rolli = FindViewById<Button>(Resource.Id.rollibutton);
    ImageButton menu = FindViewById<ImageButton> (Resource.Id.menuButton);

    CheckNetwork(); //Here you'll ask if you have connection or not and Toast will show