[英]InstantiationException: Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public
I have a project that I originally developed using Android Studio. 我有一个项目,我最初使用Android Studio开发。 I decided to convert it to Xamarin (Visual Studio 2015).
我决定将它转换为Xamarin(Visual Studio 2015)。
After hours of porting all the code over, everything works except for my Settings activity (PreferenceActivity). 移植所有代码几个小时后,除了我的设置活动(PreferenceActivity)之外,一切都有效。 I have a few PreferenceFragments that make up the settings, but all of them give me "Unable to instantiate fragment".
我有一些构成设置的PreferenceFragments,但是所有这些都让我“无法实例化片段”。 Here is the exception I am getting:
以下是我得到的例外情况:
Java.Lang.RuntimeException: Unable to start activity ComponentInfo{test.mypackagename/md50d00e677e41fc49f8b3c16e79df2b77f.SettingsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment test.mypackagename.GeneralPreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public
Java.Lang.RuntimeException:无法启动活动ComponentInfo {test.mypackagename / md50d00e677e41fc49f8b3c16e79df2b77f.SettingsActivity}:android.app.Fragment $ InstantiationException:无法实例化片段test.mypackagename.GeneralPreferenceFragment:确保类名存在,是公共的,并且具有一个公共的空构造函数
I have been looking online for a solution but I just cant seem to find one. 我一直在网上寻找解决方案,但我似乎无法找到一个。 Everywhere I look they say make sure there is an empty public constructor, if its an inner class it has to be static.
无论我到哪里,他们都会说确保有一个空的公共构造函数,如果它是一个内部类,它必须是静态的。 But I have the empty constructor and its not an inner class, its in its own file.
但我有空的构造函数,而不是内部类,它在自己的文件中。
Here is my SettingsActivity.cs: 这是我的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: 这是我的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: 这是我的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 was working fine before so Im not sure what the issue could be. 这之前工作正常,所以我不确定问题是什么。 Any help would be much appreciated.
任何帮助将非常感激。
I think you are running into this problem because when not using the [Register]
attribute on your PreferenceFragment
then its name appended with a MD5 sum by Xamarin. 我认为你遇到了这个问题,因为当你不在
PreferenceFragment
上使用[Register]
属性时,它的名字会附加一个Xamarin的MD5和。
So in order to actually have the namespace you expect it to in the pref_headers.xml you need to attribute your class: 因此,为了在pref_headers.xml中实际拥有您期望它的命名空间,您需要为您的类赋值:
[Register("test.mypackagename.GeneralPreferenceFragment")]
public class GeneralPreferenceFragment: PreferenceFragment
{
// code here
}
EDIT: 编辑:
I've just tested the code and it works fine on my machine. 我刚刚测试了代码,它在我的机器上工作正常。 I am not using any support packages or anything.
我没有使用任何支持包或任何东西。
pref_general.xml 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 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 SettingsActivity.cs
[Activity(Label = "SettingsActivity")]
public class SettingsActivity : PreferenceActivity
{
public override void OnBuildHeaders(IList<Header> target)
{
LoadHeadersFromResource(Resource.Xml.pref_headers, target);
}
}
GeneralPreferenceFragment.cs GeneralPreferenceFragment.cs
[Register("test.mypackagename.GeneralPreferenceFragment")]
public class GeneralPreferenceFragment : PreferenceFragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
AddPreferencesFromResource(Resource.Xml.pref_general);
}
}
This works fine and the app shows up the SettingsActivity
with first a general option, after clicking on that it shows up a Title and CheckBox
. 这工作正常,应用程序首先显示
SettingsActivity
一般选项,点击它后显示一个标题和CheckBox
。
This worked even without providing any ctor to the GeneralPreferenceFragment
. 即使没有向
GeneralPreferenceFragment
提供任何ctor,这也可以工作。 However, you could try add these: 但是,您可以尝试添加以下内容:
public GeneralPreferenceFragment()
{
}
public GeneralPreferenceFragment(IntPtr javaRef, JniHandleOwnership transfer)
: base(javaRef, transfer)
{
}
The latter ctor is often needed when the app is coming back from background or when the Java world is invoking the class somehow. 当应用程序从后台返回或者Java世界以某种方式调用类时,通常需要后者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.