如何在gwt jsni方法中使用int值 [英] How to work with int values in gwt jsni methods

查看:88
本文介绍了如何在gwt jsni方法中使用int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jsni方法中如何处理int值?


$ b

  public class Person扩展JavaScriptObject {

//一些其他方法

public final void setPoints(int i)/ * - {
this.points = this.points + i ;
} - * /;

public final int getPoints()/ * - {
return this.points;
} - * /;
}

我在一个与JsArray结合使用的方法中使用

  public static boolean moreThanZeroPoints(JsArray< Person> arr,int index){
if(arr.get(index).getPoints() > 0){
return true;
}
返回false;



$ b $ arr.get(index).getPoints( )给出以下错误消息:

 未捕获:发生异常:异常:已捕获:其他而不是从JSNI方法返回的int。 
package-path :: getPoints():类型为undefined的JS值,预期为int

对于 arr.get(index).setPoints(1)我得到相同的错误信息。
出了什么问题?
请帮助。

解决方案

因为 points 可能不存在,你必须将 undefined 强制转换为 getPoints()中的某个整数值。或者添加一个 hasPoints()方法,并且只有在点数时调用 getPoints() / code>被定义。

//将undefined(或null或0)强制为0
public final int getPoints()/ * - {
return this.points || 0;
} - * /;



  //将undefined或null强制为-1 
public final int getPoints()/ * - {
return(this.points!= null)? this.points:-1;
} - * /;

对于 setPoints()返回类型 int 但从来没有实际 return 任何东西,这相当于返回 undefined JS中的。 qben编辑你的答案,并通过将返回类型改为 void 来修复它。


How is it possible to work with int values in jsni methods?

public class Person extends JavaScriptObject{                                                                                                                     

   // some other methods

   public final native void setPoints(int i)/*-{
       this.points= this.points + i;
   }-*/;

   public final native int getPoints()/*-{
        return this.points;
   }-*/;                                                                                    
}

I am using this in a method combined with a JsArray

public static boolean moreThanZeroPoints(JsArray<Person> arr, int index){
     if(arr.get(index).getPoints() > 0){
         return true;
     }
     return false;
}

In arr.get(index).getPoints() gives the following error-message:

    uncaught: Exception caught: Exception: caught: something other than an int was returned from JSNI method.
    package-path:: getPoints(): JS value of type undefined, expected int

For arr.get(index).setPoints(1) i get the same error-message. What is wrong? Please help.

解决方案

Because points might not exist, you have to coerce undefined into some integer value in getPoints(); either that or add an hasPoints() method and only call getPoints() when points is defined.

// coerce undefined (or null, or 0) to 0
public final native int getPoints() /*-{
  return this.points || 0;
}-*/;

or

// coerce undefined or null to -1
public final native int getPoints() /*-{
  return (this.points != null) ? this.points : -1;
}-*/;

For setPoints(), you had declared a return type of int but never actually returned anything, which is equivalent to returning undefined in JS. qben edited your answer and fixed it by changing the return type to void.

这篇关于如何在gwt jsni方法中使用int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆