2011-09-29 94 views
0

我正在開發黑莓應用程序。我在屏幕上有一張地圖。我想用我從Web服務獲得的新數據刷新地圖數據。我在線程中使用BlockingSenderDestination。當我請求「獲取數據」時,它返回新的數據。沒問題。我正在使用invokelater函數通過傳遞參數調用我的maprefresh函數,但我得到了非法的錯誤。用新數據更新黑莓屏幕

任何建議來解決我的問題或任何更好的方式來做到這一點?

這裏是我的代碼:

public class MyMainScreen extends MainScreen { 
    RichMapField map; 
    MyClassList _myclassList; 
    private String _result2t; 

    public MyMainScreen(JSONArray jarray) 
    { 


    map = MapFactory.getInstance().generateRichMapField(); 
     MapDataModel mapDataModel = map.getModel(); 

     JSONObject json = null; 
      boolean getdata=false; 
      for (int i=0;i<jarray.length();i++) 
      { 

       try 
       { 
      json=jarray.getJSONObject(i); 
       getdata=true; 
       } 
       catch(Exception e) 
       { 

       } 

       if(getdata) 
       { 
        try 
        {      
        double lat = Double.valueOf(json.getString("LATITUDE")).doubleValue(); 
       double lng = Double.valueOf(json.getString("LONGITUDE")).doubleValue();    
       String myclassdata= json.getString("myclassdata").toString(); 
       MyClass ben = new MyClass(myclassdata);     
       _myclassList.addElement(ben);  
       MapLocation termimapitem = new MapLocation(lat, lng, "",""); 
       mapDataModel.add((Mappable)termimapitem,"1"); 
        } 

        catch(Exception e) 
        { 
         //mesajGoster("Hatalı Veri"); 
        } 

       } 
       else 
       { 
        //mesajGoster("Listeye Eklenemedi"); 
       } 
      } 
    } 


    private void GetTerminals(String companyNo){ 
     final String companyNoR= companyNo; 
     Thread t = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       Message response = null; 
       String uriStr = "http://webservice";     
       BlockingSenderDestination bsd = null; 
       try 
       { 
        bsd = (BlockingSenderDestination) 
           DestinationFactory.getSenderDestination 
            ("o", URI.create(uriStr)); 
        if(bsd == null) 
        { 
         bsd = 
          DestinationFactory.createBlockingSenderDestination 
           (new Context("o"), 
           URI.create(uriStr) 
           ); 
        } 

        response = bsd.sendReceive(); 

        if(response != null) 
        { 
         BSDResponse(response,companyNoR); 
        } 
       } 
       catch(Exception e) 
       { 
       } 
       finally 
       { 
        if(bsd != null) 
        { 
         bsd.release(); 
        } 
       } 
      } 

     }); 
     t.start(); 

    } 

    private void BSDResponse(Message msg,final String companyNo) 
    { 

     if (msg instanceof ByteMessage) 
     { 
      ByteMessage reply = (ByteMessage) msg; 
      _result2t = (String) reply.getStringPayload(); 
     } else if(msg instanceof StreamMessage) 
     { 
      StreamMessage reply = (StreamMessage) msg; 
      InputStream is = reply.getStreamPayload(); 
      byte[] data = null; 
      try { 
       data = net.rim.device.api.io.IOUtilities.streamToBytes(is); 
      } catch (IOException e) { 
       // process the error 
      } 
      if(data != null) 
      { 
       _result2t = new String(data); 
      } 
     } 
     try { 
       final JSONArray jarray= new JSONArray(_result2t);    
      final String username=_userName; 
      UiApplication.getUiApplication().invokeLater(new Runnable() { 
        public void run() { 
         Dialog.alert("The Toolbar i"); 
         Yenile(jarray); 
        } 

       }); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 



    private void Yenile(JSONArray jarray){ 
     MapDataModel mapDataModel = map.getModel(); 
     mapDataModel.remove("1"); 
     map.getMapField().update(true); 
     _terminalList = new TerminalList(); 
     map= MapFactory.getInstance().generateRichMapField(); 
     MapDataModel mapDataModel = map.getModel(); 
     JSONObject json = null; 
     boolean getdata=false; 
     for (int i=0;i<jarray.length();i++) 
     { 

      try 
      { 
     json=jarray_terminaller.getJSONObject(i); 
      getdata=true; 
      } 
      catch(Exception e) 
      { 

      } 

      if(getdata) 
      { 
       try 
       {      

       double lat = Double.valueOf(json.getString("LATITUDE")).doubleValue(); 
       double lng = Double.valueOf(json.getString("LONGITUDE")).doubleValue(); 

       String myclassdata= json.getString("myclassdata").toString(); 
       MyClass ben = new MyClass(myclassdata);     
       _myclassList.addElement(ben);  
       MapLocation termimapitem = new MapLocation(lat, lng, "",""); 
       mapDataModel.add((Mappable)termimapitem,"1"); 
       } 

       catch(Exception e) 
       { 
        //mesajGoster("Hatalı Veri"); 
       } 

      } 
      else 
      { 
       //mesajGoster("Listeye Eklenemedi"); 
      } 
     } 


    } 
} 
+1

使用調試器指向你得到錯誤的那一行。 –

回答

0

我找到了解決我的問題。

讓我通過了新的運行方法,它發送數據後:

UiApplication.getUiApplication().invokeLater(new Runnable() { 
public void run() { 
     MyFunction(jarray); 
     }}); 

,但我需要與主線程同步。所以解決方案如下:

UiApplication.getUiApplication().invokeLater(new Runnable() { 
       public void run() { 
        synchronized(Application.getEventLock()) { 
         Yenile(jarray); 
        } 

       } 

      }); 
0

要刷新畫面:這樣做:

public class LoadingScreen extends MainScreen{ 
LoadingScreen() 
{ 
    createGUI(); 
} 
public void createGUI() 
{ 
    //Here you write the code that display on screen; 
}} 

我們知道這是一個創建屏幕的實際方式;

現在,如果你想刷新屏幕寫象下面這樣:

deleteAll(); 
invalidate(); 
createGUI();//here it creates the screen with new data. 

而是寫在的invokeLater方法更好地寫出上面的三條線中的run方法的Thread.sleep之後(10000);

如果您對stackOverFlow聊天室名稱「Life for Blackberry」有任何疑問,請澄清您和我們的疑惑。