RCP Editor 常見錯誤處理方法

       最近學習RCP遇到不少問題,解決後覺得應該寫點東西與同道朋友們分享一下。筆者也是剛剛接觸RCP如文章中有錯誤歡迎執政。本文只涉及報錯信息及解決方法,希望高手能夠解釋深層次的原因。
                                                                                                                                                                                                      
"org.eclipse.ui.PartInitException: Unable to open editor, unknown editor ID: package.class"、"org.eclipse.ui.PartInitException: Editor initialization failed: package.class.  Site is incorrect."及"org.eclipse.core.runtime.AssertionFailedException: null argument:A part's title tool tip must be non-null"處理方法


1."org.eclipse.ui.PartInitException: Unable to open editor, unknown editor ID: package.class"處理方法
報錯原因:plugin.xml中<extension>標籤中沒有icon項
   <extension
         point="org.eclipse.ui.editors">
      <editor
            class="testrcp.testeditor"
            id="testrcp.testeditor"
            name="New EditorPart">
      </editor>
   </extension>
解決方法:plugin.xml中<extension>加入icon,該項可以爲空。
   <extension
         point="org.eclipse.ui.editors">
      <editor
            class="testrcp.testeditor"
            icon=""
            id="testrcp.testeditor"
            name="New EditorPart">
      </editor>
   </extension>

                                                                                                                                                                                                     
2."org.eclipse.ui.PartInitException: Editor initialization failed: package.class.  Site is incorrect."處理方法
報錯原因:EditorPart類的init()未實現
package testrcp;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

public class testeditor extends EditorPart {

    public static final String ID = "testrcp.testeditor"; //$NON-NLS-1$

    /**
     * Create contents of the editor part
     * @param parent
     */
    //@Override
   
    public void createPartControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        //
    }

    @Override
    public void setFocus() {
        // Set the focus
    }

    @Override
    public void doSave(IProgressMonitor monitor) {
        // Do the Save operation
    }

    @Override
    public void doSaveAs() {
        // Do the Save As operation
    }

    @Override
    public void init(IEditorSite site, IEditorInput input)
            throws PartInitException {
           
    }

    @Override
    public boolean isDirty() {
        return false;
    }

    @Override
    public boolean isSaveAsAllowed() {
        return false;
    }

}
解決方法:實現init()方法如下  
    public void init(IEditorSite site, IEditorInput input)
            throws PartInitException {
       
        System.out.println(input.toString());
        this.setInput(input);
        this.setSite(site);
   
    }
   
                                                                                                                                                                                                     
3."org.eclipse.core.runtime.AssertionFailedException: null argument:A part's title tool tip must be non-null"處理方法
報錯原因:EditorInput類的getToolTipText()返回值爲null  
package testrcp;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableElement;

public class testEditorInput implements IPathEditorInput {

    public testEditorInput() {
        // TODO Auto-generated constructor stub
        super();
       
    }

    @Override
    public IPath getPath() {
        // TODO Auto-generated method stub
        return null;
    }

   
    @Override
    public boolean exists() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public ImageDescriptor getImageDescriptor() {
        // TODO Auto-generated method stub
        return null;//WorkbenchImages.getImageDescriptor("");
    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return "testEditorInput";
    }

    @Override
    public IPersistableElement getPersistable() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getToolTipText() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getAdapter(Class adapter) {
        // TODO Auto-generated method stub
        return null;
    }

}
解決方法:重寫getToolTipText()方法return值可以爲""或任意字符類型。
    public String getToolTipText() {
        // TODO Auto-generated method stub
        return "";
    }
發佈了18 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章