【问题标题】:SWT application crashes in Windows 7SWT 应用程序在 Windows 7 中崩溃
【发布时间】:2010-01-06 17:09:35
【问题描述】:

我有一个可以在 Windows XP 和 Windows Vista 上正常工作的 java SWT 应用程序。但是当我在 Windows 7 上运行它时,出现了奇怪的错误,并且它崩溃了。

例如,在我调用Table.removeAll() 的方法中,我得到java.lang.ArrayIndexOutOfBoundsException: 0。该表的样式为SWT.VIRTUAL

另一个问题是当我在文本框中写一些东西时(有一个 ModifyListener 过滤一些东西) - 在 2 个字符之后,光标移动到开头(在第一个字符之前)。

SWT 版本是 3.5,但我尝试了 eclipse 网站上的最新版本(3.6M3),结果是一样的。

是否有任何已知问题?有没有人遇到过这个?


这是一个在 Windows 7 中不起作用的 sn-p:

import java.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class TableCheck {

  Shell shell;

  Button button1, button2, button3;

  UltraTable ut;

  public TableCheck() {
    Display display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout(4, false));
    Text text = new Text(shell, SWT.SINGLE | SWT.LEAD | SWT.READ_ONLY | SWT.BORDER);
    text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    text.setText("SWT " + SWT.getPlatform() + " " + SWT.getVersion() + "; "
            + System.getProperty("os.name") + " " + System.getProperty("os.version") + " "
            + System.getProperty("os.arch"));

    SelectionAdapter listener = new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            if (e.widget == button1) {
                ut.setContent(Arrays.asList("01", "34", "test", "test2", "123", "1test", "test2"));
            } else if (e.widget == button2) {
                ut.setContent(Arrays.asList("Str1", "Str2", "Str3"));
            } else {
                ut.setContent(Collections.emptyList());
            }
        }
    };
    button1 = new Button(shell, SWT.PUSH);
    button1.setText("Data 1");
    button1.addSelectionListener(listener);
    button2 = new Button(shell, SWT.PUSH);
    button2.setText("Data 2");
    button2.addSelectionListener(listener);
    button3 = new Button(shell, SWT.PUSH);
    button3.setText("Data 3");
    button3.addSelectionListener(listener);

    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1);
    gd.widthHint = gd.heightHint = 320;
    ut = new UltraTable(shell, SWT.MULTI | SWT.BORDER | SWT.VIRTUAL | SWT.FULL_SELECTION);
    ut.table.setLayoutData(gd);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
  }

  class UltraTable {
    private Table table;

    private static final String DATA_COLLECTION = "<<CC>>", DATA_LISTENER = "<<VL>>";

    public UltraTable(Composite parent, int style) {
        table = new Table(parent, style);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);
        TableColumn tableColumn = new TableColumn(table, SWT.NONE);
        tableColumn.setText("Column");
        tableColumn.setWidth(64);
    }

    void setContent(Collection<?> collection) {
        if ((table.getStyle() & SWT.VIRTUAL) != 0) {
            table.setData(DATA_COLLECTION, collection);
            table.clearAll();
            table.setItemCount(collection.size());
            if (table.getData(DATA_LISTENER) == null) {
                Listener listenerSD = new Listener() {
                    public void handleEvent(Event event) {
                        Collection<?> collectionW = (Collection<?>) event.widget.getData(DATA_COLLECTION);
                        Object object = collectionW.toArray(new Object[collectionW.size()])[event.index];
                        ((TableItem) event.item).setText(0, object.toString());
                    }
                };
                table.setData(DATA_LISTENER, listenerSD);
                table.addListener(SWT.SetData, listenerSD);
            }
        }
    }
  }
  public static void main(String[] args) {
    new TableCheck();
  }
}

我检查的SWT版本是(如shell中的文字所示):

SWT win32 3550; Windows 7 6.1 x86
SWT win32 3617; Windows 7 6.1 x86

【问题讨论】:

    标签: windows-7 swt


    【解决方案1】:

    我在asking the same question之后解决了这个问题。

    您必须删除 SWT.VIRTUAL 或更新到 SWT 的 SVN 版本。我已经在 SWT 错误页面上发布了a bug report

    编辑:您的代码 sn-p 中存在导致ArrayIndexOutOfBoundsException 的错误。您可以通过更改包含listenerSD 的行来解决它:

    public void handleEvent(Event event) {
        Collection<?> collectionW = (Collection<?>) event.widget
                .getData(DATA_COLLECTION);
        if (collectionW.size() > event.index) {
            Object object = collectionW
                    .toArray(new Object[collectionW.size()])[event.index];
            ((TableItem) event.item).setText(0, object
                    .toString());
        }
    }
    

    【讨论】:

    • 感谢您的回答。我之前在 SO 上进行过搜索,但我没有找到你的问题,因为在我的情况下它不是 StackOverflowError 并且你没有添加 windows-7 标签
    • 我将标签添加到我的问题中,以便其他用户可以更轻松地找到它。 Eclipse 上的错误报告没有明确说明StackOverflowError(除了我发布堆栈跟踪的地方),只是说存在 SWT 锁定的情况。你的具体症状是什么?也许在错误站点上发布一个最小的工作示例将有助于 SWT 开发人员追踪问题的根源。
    • 我会用这种情况做一个sn-p,因为(我认为)补丁不能解决问题。问题是我没有 Windows 7,很难找到和测试导致这些问题的确切方法。
    • Data 1 将元素插入到表格中。随后按下Data 2Data 3 会完全按照您所说的那样崩溃:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    • 但是,错误似乎在您的代码中。只需将collectionW 上的操作包装在以下 if 语句中:if (collectionW.size() &gt; event.index)
    【解决方案2】:

    在 Windows 7 上,当调用 clearAll() 时,表格会立即更新可见项。所以
    table.clearAll();
    必须在行之后
    table.setItemCount(collection.size());

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多