PreferencePage的小问题花了大把时间
在Eclipse里可以用FieldEditorPreferencePage简化Preference page的创建,但在随机文档里只重点说了要实现createFieldEditors方法,我明明实现了它,但点Preference命令时却总是提示:
java.lang.InstantiationException: org.haree.mobject.ui.BasicPreferencePage
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:141)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:124)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:113)
at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:189)
at org.eclipse.ui.internal.dialogs.WorkbenchPreferenceNode.createPage(WorkbenchPreferenceNode.java:60)
at org.eclipse.jface.preference.PreferenceDialog.showPage(PreferenceDialog.java:985)
at org.eclipse.jface.preference.PreferenceDialog$8.selectionChanged(PreferenceDialog.java:529)
at org.eclipse.jface.viewers.StructuredViewer$3.run(StructuredViewer.java:450)
at org.eclipse.core.internal.runtime.InternalPlatform.run(InternalPlatform.java:616)
at org.eclipse.core.runtime.Platform.run(Platform.java:747)
at org.eclipse.jface.viewers.StructuredViewer.firePostSelectionChanged(StructuredViewer.java:448)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1094)
at org.eclipse.jface.preference.PreferenceDialog.selectSavedItem(PreferenceDialog.java:807)
at org.eclipse.jface.preference.PreferenceDialog$3.run(PreferenceDialog.java:309)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:69)
at org.eclipse.jface.preference.PreferenceDialog.createContents(PreferenceDialog.java:305)
at org.eclipse.jface.window.Window.create(Window.java:348)
at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:925)
at org.eclipse.ui.internal.OpenPreferencesAction.run(OpenPreferencesAction.java:70)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:881)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:915)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:866)
at org.eclipse.jface.action.ActionContributionItem$7.handleEvent(ActionContributionItem.java:785)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:82)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:796)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2772)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2431)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:141)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:124)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:113)
at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:189)
at org.eclipse.ui.internal.dialogs.WorkbenchPreferenceNode.createPage(WorkbenchPreferenceNode.java:60)
at org.eclipse.jface.preference.PreferenceDialog.showPage(PreferenceDialog.java:985)
at org.eclipse.jface.preference.PreferenceDialog$8.selectionChanged(PreferenceDialog.java:529)
at org.eclipse.jface.viewers.StructuredViewer$3.run(StructuredViewer.java:450)
at org.eclipse.core.internal.runtime.InternalPlatform.run(InternalPlatform.java:616)
at org.eclipse.core.runtime.Platform.run(Platform.java:747)
at org.eclipse.jface.viewers.StructuredViewer.firePostSelectionChanged(StructuredViewer.java:448)
at org.eclipse.jface.viewers.StructuredViewer.setSelection(StructuredViewer.java:1094)
at org.eclipse.jface.preference.PreferenceDialog.selectSavedItem(PreferenceDialog.java:807)
at org.eclipse.jface.preference.PreferenceDialog$3.run(PreferenceDialog.java:309)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:69)
at org.eclipse.jface.preference.PreferenceDialog.createContents(PreferenceDialog.java:305)
at org.eclipse.jface.window.Window.create(Window.java:348)
at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:925)
at org.eclipse.ui.internal.OpenPreferencesAction.run(OpenPreferencesAction.java:70)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:881)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:915)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:866)
at org.eclipse.jface.action.ActionContributionItem$7.handleEvent(ActionContributionItem.java:785)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:82)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:796)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:2772)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2431)
找了一个多小时,才发现原来是要提供一个没有参数的构造方法才可以。想了一下,可能因为Eclipse最终是使用Class.newInstance()的方式来生成界面元素的,所以有这个要求。
算是一个经验吧,我觉得Eclipse的帮助再丰富些就好了,特别是应该提供更多的例子代码。
顺便推荐下面两篇文章(要是Eclipse帮助都这么详细该多好):