0

我正在尝试使用 Set。但是我在第一次出现 add() 函数时出错.. -innerSetException(futurTask.java:273) -setException(FutureTask.java:124)。所以我真的不明白......我真的希望你能帮助我!这是我的代码:

    public class RetrieveArticleTask extends AsyncTask<Void, Void, Set<Article>> {

private List<Article> articles;
private List<Article> products ;
private Set<Article> productsUpdates;

private LoginTaskListener  callbackArticlesReceived;

public RetrieveArticleTask(AutoCompletionActivity  callback) {
    this.articles  = new ArrayList<Article>(200);
    this.products  = new ArrayList<Article>(200);
    this.productsUpdates = new TreeSet<Article>();

    this.callbackArticlesReceived = callback;           
}               

@Override
protected Set<Article> doInBackground(Void... aVoid) {

    try {
        List<ArticleResult> articleResults = RetailReloadWebServicesArticle.getListArticlesAll();

        for(ArticleResult articleResult : articleResults ){ 
            articles.add(new Article(articleResult));
            System.out.println(productsUpdates.size());
            productsUpdates.add(new Article(articleResult));
        }
        System.out.println("article size : " + articles.size());
    } catch (ITException e) {
        e.printStackTrace();
        System.out.println("ERROR   :::" +e);
    }


    try {
        products = RetailReloadApplication.getSqliteOpenHelper().getArticleDao().getAllObjects();
    } catch (ITException e) {
        e.printStackTrace();
    }
    System.out.println(products.size());
    for(Article product : products ){ 
        productsUpdates.add(product);
    }



    try {
        if(productsUpdates != null){
            for(Article productUpdate : productsUpdates ){ 
                RetailReloadApplication.getSqliteOpenHelper().getArticleDao().addOrUpdateObject(productUpdate);
            }
        }
    } catch (ITException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return productsUpdates;
}

@Override
protected void onPostExecute(Set<Article> articles) {
    System.out.println(articles.size());
    if (null != callbackArticlesReceived)
        callbackArticlesReceived.onLoginTaskFinished(articles);
}

}

这里是完整的堆栈跟踪:所以这里是堆栈跟踪:

07-04 08:13:17.322: E/AndroidRuntime(652): java.lang.RuntimeException: An error occured while executing doInBackground()
07-04 08:13:17.322: E/AndroidRuntime(652):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-04 08:13:17.322: E/AndroidRuntime(652):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.lang.Thread.run(Thread.java:856)
07-04 08:13:17.322: E/AndroidRuntime(652): Caused by: java.lang.ClassCastException: com.retailreload.service.model.result.article.ArticleResult is not Comparable
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.TreeMap.find(TreeMap.java:254)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.TreeMap.putInternal(TreeMap.java:240)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.TreeMap.put(TreeMap.java:186)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.TreeSet.add(TreeSet.java:113)
07-04 08:13:17.322: E/AndroidRuntime(652):  at com.retailreload.task.RetrieveArticleTask.doInBackground(RetrieveArticleTask.java:48)
07-04 08:13:17.322: E/AndroidRuntime(652):  at com.retailreload.task.RetrieveArticleTask.doInBackground(RetrieveArticleTask.java:1)
07-04 08:13:17.322: E/AndroidRuntime(652):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-04 08:13:17.322: E/AndroidRuntime(652):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-04 08:13:17.322: E/AndroidRuntime(652):  ... 5 more
4

1 回答 1

0

您正在将不可比较的对象添加到 TreeSet。当您将对象添加到 TreeSet 时,该 TreeSet 将自动排序,而这里 java 找不到对您的 Article 对象进行排序的方法,因为您似乎没有提供。

如果您不关心排序,请将 TreeSet 更改为 HashSet。如果您关心它,Article 类必须实现 Comparable 接口,或者您可以在构建 TreeSet 时提供一个 Comparator 对象。

请参阅 TreeSet文档

这是一个示例文章类:

public class Article implements Comparable<Article> {
    private String name;
    private int price;

    //Constructors, getters and setters

    @Override
    public int compareTo(Article a) {
        return this.name.compareTo(a.name);
    }
}

此处文章对象将按名称排序。compareTo 方法必须返回一个整数:

  • 如果“this”文章小于提供的“a”,则为负数
  • 如果等于则为零
  • 大于“a”时为正

使用您需要的排序逻辑实现该接口和 compareTo 方法,它应该可以工作。

于 2013-07-04T09:03:54.470 回答