2011-09-16 113 views
0

我需要知道是否有方法可以說在Java Android中找不到int類型。 我正在寫一個android應用程序,它實際上是爲iPhone寫的,並且有一個小問題。在某些時候,我必須檢查返回方法的int類型是否未找到,以及是否爲真,然後進行一些計算。問題是,當我這樣做,在Java作爲if(Id==0)它拋出我即使ID爲0Java中的NSNotFound相當於

這是我的方法異常:

private static int localUserIdByServerUserId(int serverUserId, String serverName){ 

    dbHelper = new DataBaseHelper(context, "stampii_sys_tpl.sqlite", null, 1); 
    dbHelper.getDatabase(); 

    String query = "SELECT id FROM users WHERE objectId = "+serverUserId+" AND serverName = '"+serverName+"' LIMIT 1"; 
    ArrayList<String> result = new ArrayList<String>(); 
    cursor = dbHelper.executeSQLQuery(query); 
    cursor.moveToFirst(); 
    while(!cursor.isAfterLast()) { 
     result.add(cursor.getString(cursor.getColumnIndex("id"))); 
     cursor.moveToNext(); 
    } 

    Log.i("result ","Result : "+result.toString()); 
    Log.i("CURSOR ","Cursor Position : "+cursor.getPosition()); 
    int uuid = Integer.parseInt(result.get(cursor.getColumnIndex("objectId")+1)); 
    Log.w("localUSerByIdServerUserId","LocalUserByIdServerUserId result : "+uuid); 
    cursor.close(); 
    return uuid; 

,這裏是如何我使用它:

int uuId = rpc.lUserIdByServerUserId(userId,newServerName); 
     Log.w("uuId","uuId : "+uuId); 
     if(uuId==0){ 
      // do some calculations 
     } 

有什麼建議嗎?

+1

您可以發佈您正在獲取的異常的堆棧跟蹤嗎? – theisenp

回答

2

lUserIdByServerUserId()當用戶找不到時可能會引發異常。

例如

try { 
    int uuId = rpc.lUserIdByServerUserId(userId,newServerName); 
    Log.w("uuId","uuId : "+uuId); 
} 
catch (NotFoundException nfe) { 
    // do some calculations 
} 

您需要修改lUserIdByServerUserId()才能拋出異常。如果Java庫中尚不存在合適的異常,您可能還需要定義自己的NotFoundException類。

編輯: 另外,從@ mthpvg的回答以下,你可以改變lUserIdByServerUserId()返回一個Integer類型,可如果沒有找到和測試設置爲null

例如

Integer uuId = rpc.lUserIdByServerUserId(userId,newServerName); 
if (uuId == null) { 
    // Do some calculations 
}