-4

Creating with visual studio a skeleton app with tabbed views. One tab is for settings but having trouble using preferences api. I get this error :

android.view.View cannot be cast to android.view.ViewGroup

Here's the code:

//***********************
//class for settings tab
//***********************
    [Activity(Label = "Settings")]
    [MetaData(PreferenceManager.MetadataKeyPreferences, Resource = "@drawable/preferences")]
    public class Settings : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);
            View vg = new View(this);
            TextView textview = new TextView(vg.Context);
            textview.Text = "This is the Settings Tab";
            SetContentView(vg);
            textview.Id = TextView.GenerateViewId();
            vg.Id = View.GenerateViewId();
            FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
            SettingsFragment cfg = new SettingsFragment();

            fragmentTx.Add(vg.Id, cfg);

            // Commit the transaction.
            fragmentTx.Commit();
        }
    }

    public class SettingsFragment : PreferenceFragment
    {

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            AddPreferencesFromResource(Resource.Xml.preferences);
            /*var intent = new Intent(this.Activity, typeof(Settings));
            AddPreferencesFromIntent(intent);*/
        }



    }
  • Just asked if there's something similar posted somewere! Reading guides seems all clear to me but in practice i get struck! will post more specific questions to reach my goal. :-) – user3546624 Apr 04 '17 at 20:05

1 Answers1

0

Found! Replaced View with a FrameLayout:

protected override void OnCreate(Bundle savedInstanceState) {

    base.OnCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    SetContentView(frame);
    frame.Id = FrameLayout.GenerateViewId();
    FragmentTransaction fragmentTx = this.FragmentManager.BeginTransaction();
    SettingsFragment cfg = new SettingsFragment();
    fragmentTx.Add(frame.Id, cfg);
    // Commit the transaction.
    fragmentTx.Commit();
}