2014-01-22 13 views
2

運行我的SWT應用程序後,它在編譯時沒有任何錯誤。 但運行後它會顯示幾秒的輸出,然後eclipse實例將不會響應。 請幫我避免異常 我嘗試增加堆size.anybody在這裏幫助我..... plzzzzzzzzzzzzzzzzzzzz運行Eclipse SWT應用程序後,它顯示了許多例外

下面我給我的代碼。我認爲我的邏輯也有問題。但我不知道如何糾正它。我只是按照書上做這件事。它是一個時鐘程序。而且我在每一次秒針動作中都會創建一個新的線程。我想把它作爲一個線程。 它顯示無法創建新的本地線程 activator.java

package com.packtpub.e4.clock.ui; 

import org.eclipse.jface.resource.ImageDescriptor; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.events.SelectionListener; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.graphics.RGB; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Tray; 
import org.eclipse.swt.widgets.TrayItem; 
import org.eclipse.ui.plugin.AbstractUIPlugin; 
import org.osgi.framework.BundleContext; 

/** 
    * The activator class controls the plug-in life cycle 
*/ 
    public class Activator extends AbstractUIPlugin { 

// The plug-in ID 
public static final String PLUGIN_ID = "com.packtpub.e4.clock.ui"; //$NON-NLS-1$ 

// The shared instance 
private static Activator plugin; 
private TrayItem trayItem; 
private Image image; 
private Shell shell; 



/** 
* The constructor 
*/ 
public Activator() { 
} 

/* 
* (non-Javadoc) 
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) 
*/ 
public void start(BundleContext context) throws Exception { 
    super.start(context); 
    plugin = this; 
    final Display display = Display.getDefault(); 
    display.asyncExec(new Runnable() { 
    public void run() { 

     image = new Image(display, Activator.class.getResourceAsStream("/icons/sample.gif")); 
       Tray tray = display.getSystemTray(); 
       if (tray != null && image != null) { 
       trayItem = new TrayItem(tray, SWT.NONE); 
       trayItem.setToolTipText("Hello World"); 
       trayItem.setVisible(true); 
       trayItem.setText("Hello World"); 
       trayItem.setImage(new Image(trayItem.getDisplay(), 
       Activator.class.getResourceAsStream("/icons/sample.gif"))); 
       } 
       trayItem.addSelectionListener(new SelectionListener() { 
        public void widgetSelected(SelectionEvent e) { 
        if (shell == null) { 
        shell = new Shell(trayItem.getDisplay()); 
        shell.setLayout(new FillLayout()); 
        new ClockWidget(shell, SWT.NONE, new RGB(255, 0, 255)); 
        shell.pack(); 
        } 
        shell.open(); 
        } 

        @Override 
        public void widgetDefaultSelected(SelectionEvent e) { 
         // TODO Auto-generated method stub 

        } 
       }); 

       } 
       }); 




} 

/* 
* (non-Javadoc) 
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) 
*/ 
public void stop(BundleContext context) throws Exception { 

     if (trayItem != null && !trayItem.isDisposed()) { 
     Display.getDefault().asyncExec(new Runnable() { 
     public void run() { 
     if (trayItem != null && !trayItem.isDisposed()) 
      trayItem.dispose(); 
     } 
     }); 
     } 
     if (image != null && !image.isDisposed()) { 
     Display.getDefault().asyncExec(new Runnable() { 
     public void run() { 
     if (image != null && !image.isDisposed()) 
     image.dispose(); 
     } 
     }); 
     } 
     if (shell != null && !shell.isDisposed()) { 
      Display.getDefault().asyncExec(new Runnable() { 
      public void run() { 
      if (shell != null && !shell.isDisposed()) 
      shell.dispose(); 
      } 
      }); 
      } 



} 

/** 
* Returns the shared instance 
* 
* @return the shared instance 
*/ 
public static Activator getDefault() { 
    return plugin; 
} 

/** 
* Returns an image descriptor for the image file at the given 
* plug-in relative path 
* 
* @param path the path 
* @return the image descriptor 
*/ 
public static ImageDescriptor getImageDescriptor(String path) { 
    return imageDescriptorFromPlugin(PLUGIN_ID, path); 
} 

}

clockwidget.java

package com.packtpub.e4.clock.ui; 

import java.util.Date; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.DisposeEvent; 
import org.eclipse.swt.events.DisposeListener; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.graphics.RGB; 
import org.eclipse.swt.widgets.Canvas; 
import org.eclipse.swt.widgets.Composite; 

public class ClockWidget extends Canvas 
{ 
private final Color color; 
private int offset; 
public void setOffset(int offset) 
{ 
this.offset = offset; 
} 


public Color getColor() 
{ 
    return color; 
} 


public int getOffset() 
{ 
    return offset; 
} 


public ClockWidget(Composite parent, int style,RGB rgb) 
{ 
    super(parent, style); 
    this.color = new Color(parent.getDisplay(),rgb); 
    addDisposeListener(new DisposeListener() 
    { 
     public void widgetDisposed(DisposeEvent e) 
     { 
      if(color != null && !color.isDisposed()) 
       color.dispose(); 
     } 
    }); 

    addPaintListener(new PaintListener() 
    { 
     public void paintControl(PaintEvent e) 
     { 
      ClockWidget.this.paintControl(e); 
     } 
    }); 

} 

public void paintControl(PaintEvent e) 
{ 
    @SuppressWarnings("deprecation") 
    int seconds = new Date().getSeconds(); 
    int arc = (15-seconds) * 6 % 360; 
    e.gc.setBackground(color); 
    e.gc.fillArc(e.x,e.y,e.width-1,e.height-1,arc-1,2); 
    e.gc.drawArc(e.x,e.y,e.width-1,e.height-1,0,360); 
    e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK)); 
    @SuppressWarnings("deprecation") 
    int hours = new Date().getHours() + offset; 
    arc = (3 - hours) * 30 % 360; 
    e.gc.fillArc(e.x, e.y, e.width-1, e.height-1, arc - 5, 10); 

    new Thread("TickTock") 
    { 
     public void run() 
     { 
      while (!ClockWidget.this.isDisposed()) 
      { 

       ClockWidget.this.getDisplay().asyncExec(
       new Runnable() 
       { 
        public void run() 
        { 
         if (!ClockWidget.this.isDisposed()) 
          ClockWidget.this.redraw(); 

        } 
       }); 
       try 
       { 
        Thread.sleep(99999); 
       } 
       catch (InterruptedException e) 
       { 
        System.out.println("@clock"+e.toString()); 
        return; 
       } 
      } 
     } 
    }.start(); 



} 


public Point computeSize(int w,int h,boolean changed) 
{ 
    int size; 
    if(w == SWT.DEFAULT) 
    { 
     size = h; 
    } 
    else if (h == SWT.DEFAULT) 
    { 
     size = w; 
    } 
    else 
    { 
     size = Math.min(w,h); 
    } 
    if(size == SWT.DEFAULT) 
     size = 50; 
    return new Point(size,size); 
} 

} 

SampleView.java

package com.packtpub.e4.clock.ui.views; 


import java.util.TimeZone; 

import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.events.SelectionListener; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.graphics.RGB; 
import org.eclipse.swt.layout.RowLayout; 
import org.eclipse.swt.widgets.Combo; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.ui.part.*; 
import org.eclipse.swt.SWT; 

import com.packtpub.e4.clock.ui.ClockWidget; 




public class SampleView extends ViewPart { 

private Combo timezones; 



public void createPartControl(Composite parent) { 
    try{ 
    RowLayout layout = new RowLayout(SWT.HORIZONTAL); 
    parent.setLayout(layout); 
    Object[] oo=parent.getDisplay().getDeviceData().objects; 
    int c = 0; 
    for (int j = 0; j < oo.length; j++) 
    if (oo[j] instanceof Color) 
    c++; 
    System.err.println("There are " + c + " Color instances"); 

    final ClockWidget clock1 =new ClockWidget(parent, SWT.NONE, new RGB(255,0,0)); 
    //final ClockWidget clock2 =new ClockWidget(parent, SWT.NONE, new RGB(0,255,0)); 
    //final ClockWidget clock3 =new ClockWidget(parent, SWT.NONE, new RGB(0,0,255)); 

    //clock1.setLayoutData(new RowData(20,20)); 
    //clock3.setLayoutData(new RowData(100,100)); 
    String[] ids = TimeZone.getAvailableIDs(); 
    timezones = new Combo(parent, SWT.SIMPLE); 
    timezones.setVisibleItemCount(5); 
    for (int i = 0; i < ids.length; i++) { 
    timezones.add(ids[i]); 

    timezones.addSelectionListener(new SelectionListener() { 
     public void widgetSelected(SelectionEvent e) { 
     String z = timezones.getText(); 
     TimeZone tz = z == null ? null : TimeZone.getTimeZone(z); 
     TimeZone dt = TimeZone.getDefault(); 
     int offset = tz == null ? 0 : (
       tz.getOffset(System.currentTimeMillis()) - 
       dt.getOffset(System.currentTimeMillis()))/3600000; 
       clock1.setOffset(offset); 
       clock1.redraw(); 
       } 
       public void widgetDefaultSelected(SelectionEvent e) { 
       clock1.setOffset(0); 
       clock1.redraw(); 
       } 
       }); 





    } 

}catch(Exception e){ 
    System.out.println("@ SampleView.java"+e.toString()); 
} 

} 

    public void setFocus() { 
     timezones.setFocus(); 

    } 
    } 
+0

你讀[這](http://stackoverflow.com/questions/6754628/eclipse-crashes-with-unable-to-create-new-native-thread-any-想法 - 我 - setti)? – Baz

+0

如果您的內存不足,我會想象您在某處發生泄漏。請記住,與eclipse中未編譯的代碼相比,編譯後的代碼可以有不同的內存佔用量。 – Gorbles

+0

我已經編寫了內存泄漏代碼..data後也出現同樣的問題 – PathuZ

回答

1

我得到了答案。 ..這裏的線程調用在clockwidgets paintc中發生ontrol函數將它從那裏剪下並粘貼到clockwidget構造函數中。 然後代碼將正常工作 package com.packtpub.e4.clock.ui;

import java.util.Date; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.DisposeEvent; 
import org.eclipse.swt.events.DisposeListener; 
import org.eclipse.swt.events.PaintEvent; 
import org.eclipse.swt.events.PaintListener; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.graphics.RGB; 
import org.eclipse.swt.widgets.Canvas; 
import org.eclipse.swt.widgets.Composite; 

public class ClockWidget extends Canvas 
    { 
    private final Color color; 
    private int offset; 
    public void setOffset(int offset) 
{ 
this.offset = offset; 
} 


public Color getColor() 
{ 
    return color; 
} 


public int getOffset() 
{ 
    return offset; 
} 


public ClockWidget(Composite parent, int style,RGB rgb) 
{ 
    super(parent, style); 
    this.color = new Color(parent.getDisplay(),rgb); 


    addDisposeListener(new DisposeListener() 
    { 
     public void widgetDisposed(DisposeEvent e) 
     { 
      if(color != null && !color.isDisposed()) 
       color.dispose(); 
     } 
    }); 
    new Thread("TickTock") 
    { 
     public void run() 
     { 
      while (!ClockWidget.this.isDisposed()) 
      { 

       ClockWidget.this.getDisplay().asyncExec(
       new Runnable() 
       { 
        public void run() 
        { 
         if (!ClockWidget.this.isDisposed()) 
          ClockWidget.this.redraw(); 

        } 
       }); 
       try 
       { 
        Thread.sleep(1000); 
       } 
       catch (InterruptedException e) 
       { 

        return; 
       } 
      } 
     } 
    }.start(); 

    addPaintListener(new PaintListener() 
    { 
     public void paintControl(PaintEvent e) 
     { 
      ClockWidget.this.paintControl(e); 


     } 
    }); 

} 

public void paintControl(PaintEvent e) 
{ 
    @SuppressWarnings("deprecation") 
    int seconds = new Date().getSeconds(); 
    int arc = (15-seconds) * 6 % 360; 
    e.gc.setBackground(color); 
    e.gc.fillArc(e.x,e.y,e.width-1,e.height-1,arc-1,2); 
    e.gc.drawArc(e.x,e.y,e.width-1,e.height-1,0,360); 
    e.gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLACK)); 
    @SuppressWarnings("deprecation") 
    int hours = new Date().getHours() + offset; 
    arc = (3 - hours) * 30 % 360; 
    e.gc.fillArc(e.x, e.y, e.width-1, e.height-1, arc - 5, 10); 

} 


public Point computeSize(int w,int h,boolean changed) 
{ 
    int size; 
    if(w == SWT.DEFAULT) 
    { 
     size = h; 
    } 
    else if (h == SWT.DEFAULT) 
    { 
     size = w; 
    } 
    else 
    { 
     size = Math.min(w,h); 
    } 
    if(size == SWT.DEFAULT) 
     size = 50; 
    return new Point(size,size); 
} 

}

相關問題