Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's the code:
C#
namespace Quiz_App
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = false)]
    public class MainActivity : Android.Support.V7.App.AppCompatActivity
    {
        Android.Support.V7.Widget.Toolbar toolbar;
        
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            toolbar = (Android.Support.V7.Widget.Toolbar)
                          FindViewById(Resource.Id.toolbar);

            SetSupportActionBar(toolbar);
            SupportActionBar.Title = "TOPICS";
            Android.Support.V7.App.ActionBar actionBar = SupportActionBar;
            actionBar.SetHomeAsUpIndicator(Resource.Drawable.menuaction);
            actionBar.SetDisplayHomeAsUpEnabled(true);
        }
        
    }
}

the problem occurs at the (Android.Support.V7.Widget.Toolbar)FindViewById(Resource.Id.toolbar) part the moment i run in the emulator.I used Visual studio 2022 for this project.

What I have tried:

I have tried looking at various sources on google including stack over flow .
Posted
Updated 9-May-22 21:01pm
v2
Comments
Graeme_Grant 10-May-22 3:00am    
Please update your question with the error message that is thown. It will have important information.

1 solution

The error message is pretty explicit:
Specified cast is not valid
Means exactly what it says: you have an object that you are trying to cast to a different type, but the two types are wildly different and you cannot treat ObjectA as an instance of ObjectB

It's the same thing if you try to cast a string to an integer: string and integer do not share any "common ancestry" so you can't cast one to the other, you have to use the appropriate conversion methods (ToString, Convert.ToInt32, int.TryParse ...)

But we don't know what FindViewById(Resource.Id.toolbar) is returning so we can't help you work out why it's a problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900