InstantationException: unable to instantiate fragment, make sure class name exists, is public and has empty constructor, open

I have a project that I originally developed using Android Studio. I decided to convert it to Xamarin (Visual Studio 2015).

After hours of porting all the code, everything works, except for my PreferenceActivity. I have several PreferenceFragments that make up the settings, but all of them give me "Unable to instantiate fragment". Here is the exception I get:

Java.Lang.RuntimeException: Cannot start Activity ComponentInfo {test.mypackagename / md50d00e677e41fc49f8b3c16e79df2b77f.SettingsActivity}: android.app.Fragment $ InstantationException: Cannot execute instance of test.mypackagename.GeneralPreferenceFragment fragment, make sure that there is a class name that makes sure that has an empty constructor that is public

I was looking for an online solution, but I just can't find it. Wherever I look, they say that there is an empty public constructor if its inner class should be static. But I have an empty constructor and its not an internal class, but its own file.

Here are my SettingsActivity.cs:

namespace test.mypackagename
{
    public class SettingsActivity : PreferenceActivity
    {
        protected override void OnPostCreate(Bundle savedInstanceState)
        {
            base.OnPostCreate(savedInstanceState);
        }

        public override void OnBuildHeaders(IList<Header> target)
        {
            LoadHeadersFromResource(Resource.Xml.pref_headers, target);
        }
    }
}

Here is my GeneralPreferenceFragment.cs:

namespace test.mypackagename
{
    public class GeneralPreferenceFragment : PreferenceFragment
    {
        public GeneralPreferenceFragment() { }

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            AddPreferencesFromResource(Resource.Xml.pref_general);
        }
    }
}

And here is my pref_headers.xml:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

    <header android:fragment="test.mypackagename.GeneralPreferenceFragment"
        android:title="@string/pref_header_general" />

    <header android:fragment="test.mypackagename.OtherPreferenceFragment1"
        android:title="@string/pref_header_other1" />

    <header android:fragment="test.mypackagename.OtherPreferenceFragment2"
        android:title="@string/pref_header_other2" />

    <header android:fragment="test.mypackagename.OtherPreferenceFragment3"
        android:title="@string/pref_header_other3" />

    <header android:fragment="test.mypackagename.OtherPreferenceFragment4"
        android:title="@string/pref_header_other4" />

</preference-headers>

This worked well, so I'm not sure what the problem is. Any help would be greatly appreciated.

+4
1

, , , [Register] PreferenceFragment, MD5 Xamarin.

, , pref_headers.xml, :

[Register("test.mypackagename.GeneralPreferenceFragment")]
public class GeneralPreferenceFragment: PreferenceFragment
{
    // code here
}

EDIT:

, . - .

pref_general.xml

<?xml version="1.0" encoding="utf-8" ?> 
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
            android:title="durr">
        <CheckBoxPreference
                android:key="checkbox_preference"
                android:title="herp"
                android:summary="derp" />
    </PreferenceCategory>
</PreferenceScreen>

pref_headers.xml

<?xml version="1.0" encoding="utf-8" ?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
  <header android:fragment="test.mypackagename.GeneralPreferenceFragment"
      android:title="general" />
</preference-headers>

SettingsActivity.cs

[Activity(Label = "SettingsActivity")]
public class SettingsActivity : PreferenceActivity
{
    public override void OnBuildHeaders(IList<Header> target)
    {
        LoadHeadersFromResource(Resource.Xml.pref_headers, target);
    }
}

GeneralPreferenceFragment.cs

[Register("test.mypackagename.GeneralPreferenceFragment")]
public class GeneralPreferenceFragment : PreferenceFragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        AddPreferencesFromResource(Resource.Xml.pref_general);
    }
}

, SettingsActivity , CheckBox.

Are common CheckBoxPreference

- ctor GeneralPreferenceFragment. :

public GeneralPreferenceFragment()
{
}

public GeneralPreferenceFragment(IntPtr javaRef, JniHandleOwnership transfer)
    : base(javaRef, transfer)
{
}

ctor , Java - .

+6

All Articles