2016-05-23 84 views
1

我是一名Android開發人員,目前我正在嘗試開發跨平臺應用程序,我使用了閃爍演示代碼,我只複製了演示中的所有圖像和所有java文件,一切正常除了Web服務調用罰款,我當我調用Web服務在codenameone中調用web服務的空指針異常

這是Web服務的鏈接空指針異常:

http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json

異常下面的代碼

這個事
final URLImage image = URLImage.createToStorage(im, time, link, null); 

如果我從flicker演示項目中複製theme.res,我沒有遇到這個異常。

我發現它發生在theme.res 但我不能找到我要添加到列表中顯示

我添加錯誤報告。請到人幫我的數據和圖像什麼成分找到解決

Expected null for key value! 
java.lang.NullPointerException 
    at com.codename1.ui.URLImage.<init>(URLImage.java:154) 
    at com.codename1.ui.URLImage.createToStorage(URLImage.java:357) 
    at com.agarangroup.flicker.Flickerdemo.createEntry(Flickerdemo.java:264) 
    at com.agarangroup.flicker.Flickerdemo.access$3(Flickerdemo.java:254) 
    at com.agarangroup.flicker.Flickerdemo$3$1.run(Flickerdemo.java:204) 
    at com.codename1.ui.Display.processSerialCalls(Display.java:1150) 
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1094) 
    at com.codename1.ui.Display.mainEDTLoop(Display.java:995) 
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) 
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176) 

這裏我重視我的代碼

package com.agarangroup.flicker; 


import java.io.IOException; 
import java.util.List; 
import java.util.Map; 

import com.codename1.components.InfiniteProgress; 
import com.codename1.media.Media; 
import com.codename1.media.MediaManager; 
import com.codename1.ui.Button; 
import com.codename1.ui.Command; 
import com.codename1.ui.Component; 
import com.codename1.ui.Container; 
import com.codename1.ui.Dialog; 
import com.codename1.ui.Display; 
import com.codename1.ui.EncodedImage; 
import com.codename1.ui.FontImage; 
import com.codename1.ui.Form; 
import com.codename1.ui.Image; 
import com.codename1.ui.Label; 
import com.codename1.ui.TextArea; 
import com.codename1.ui.Toolbar; 
import com.codename1.ui.URLImage; 
import com.codename1.ui.animations.BubbleTransition; 
import com.codename1.ui.events.ActionEvent; 
import com.codename1.ui.events.ActionListener; 
import com.codename1.ui.layouts.BorderLayout; 
import com.codename1.ui.layouts.BoxLayout; 
import com.codename1.ui.plaf.UIManager; 
import com.codename1.ui.util.Resources; 


public class Flickerdemo { 

    private Form current; 

     private static Resources res; 

     public void init(Object context) { 
      res = UIManager.initFirstTheme("/themes"); 
      // Pro only feature, uncomment if you have a pro subscription 
      //Log.bindCrashProtection(true); 
     } 

     public void start() { 
      if (current != null) { 
       current.show(); 
       return; 
      } 
      Form main = createMainForm(); 
      main.show(); 
     } 


    @SuppressWarnings("deprecation") 
    private Form createMainForm() { 
     Form main = new Form("Flickr tags"); 
     main.setLayout(new BorderLayout()); 
     Toolbar bar = new Toolbar(); 
     main.setToolBar(bar); 
     addCommandsToToolbar(bar); 

     TextArea desc = new TextArea(); 
     desc.setText("This is a Flickr tags demo, the demo uses the Toolbar to arrange the Form Commands.\n\n" 
       + "Select \"Cats\" to view the latest photos that were tagged as \"Cats\".\n\n" 
       + "Select \"Dogs\" to view the latest photos that were tagged as \"Dogs\".\n\n" 
       + "Select \"Search\" to enter your own tags for search."); 
     desc.setEditable(false); 

     main.addComponent(BorderLayout.CENTER, desc); 
     return main; 
     } 

    private void addCommandsToToolbar(Toolbar tool) { 
     // TODO Auto-generated method stub 

     tool.addCommandToSideMenu(new Command("Main") { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 
       Form main = createMainForm(); 
       main.show(); 
      } 

     }); 


      tool.addCommandToSideMenu(new Command("Cats") { 

       @SuppressWarnings("deprecation") 
       @Override 
       public void actionPerformed(ActionEvent evt) { 
        final Form cats = new Form("Cats"); 
        cats.setLayout(new BoxLayout(BoxLayout.Y_AXIS)); 
        cats.setScrollableY(true); 
        final CustomToolbar bar = new CustomToolbar(true); 
        cats.getContentPane().addScrollListener(bar); 
        cats.setToolBar(bar); 
        addCommandsToToolbar(bar); 

        Image icon = FontImage.createMaterial(FontImage.MATERIAL_REFRESH, UIManager.getInstance().getComponentStyle("TitleCommand"));     
        bar.addCommandToRightBar(new Command("", icon) { 

         @Override 
         public void actionPerformed(ActionEvent evt) { 
          Display.getInstance().callSerially(new Runnable() { 

           public void run() { 
            updateScreenFromNetwork(cats, "cat"); 
            cats.revalidate(); 
           } 
          }); 
         } 
        }); 
        bar.addCommandToOverflowMenu(new Command("Clear ") { 

         @Override 
         public void actionPerformed(ActionEvent evt) { 
          Container cnt = (Container) cats.getContentPane(); 
          cnt.removeAll(); 
          //add back the big angry cat image 
          try { 
           Image im = Image.createImage("/cat.jpg"); 
           im = im.scaledWidth(Display.getInstance().getDisplayWidth()); 
           Label bigCat = new Label(im); 
           cats.addComponent(bigCat); 

          } catch (IOException ex) { 
           ex.printStackTrace(); 
          } 
          cnt.revalidate(); 
         } 
        }); 
        bar.addCommandToOverflowMenu(new Command("About Cats ") { 

         @Override 
         public void actionPerformed(ActionEvent evt) { 
          if (Dialog.show("Cats", "Cats are meowing", "Ok", "Cancel")) { 
           try { 
            Media m = MediaManager.createMedia(Display.getInstance().getResourceAsStream(getClass(), "/Cats.mp3"), "audio/mp3"); 
            m.play(); 
           } catch (IOException ex) { 
            ex.printStackTrace(); 
           } 
          } 
         } 

        }); 

        //add the big angry cat image 
        try { 
         Image im = Image.createImage("/cat.jpg"); 
         im = im.scaledWidth(Display.getInstance().getDisplayWidth()); 
         Label bigCat = new Label(im); 
         cats.addComponent(bigCat); 

        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 

        cats.show(); 

        updateScreenFromNetwork(cats, "cat"); 

       } 

      }); 

    } 

    public void stop() { 
     current = Display.getInstance().getCurrent(); 
     if(current instanceof Dialog) { 
      ((Dialog)current).dispose(); 
      current = Display.getInstance().getCurrent(); 
     } 
    } 

    public void destroy() { 
    } 

    private static void updateScreenFromNetwork(final Form f, final String tag) { 
     //show a waiting progress on the Form 
     addWaitingProgress(f); 

     //run the networking on a background thread 
     Display.getInstance().scheduleBackgroundTask(new Runnable() { 

      @SuppressWarnings("rawtypes") 
      public void run() { 
       final List entries = ServerAccess.getEntriesFromFlickrService(tag); 

       //build the UI entries on the EDT using the callSerially 
       Display.getInstance().callSerially(new Runnable() { 

        public void run() { 
         Container cnt = f.getContentPane(); 
         for (int i = 0; i < entries.size(); i++) { 
          Map data = (Map) entries.get(i); 
          cnt.addComponent(createEntry(data, i)); 
         } 
         f.revalidate(); 
         //remove the waiting progress from the Form 
         removeWaitingProgress(f); 
        } 
       }); 

      } 
     }); 

    } 

    private static void addWaitingProgress(Form f) { 
     addWaitingProgress(f, true, f.getContentPane()); 
    } 

    private static void addWaitingProgress(Form f, boolean center, Container pane) { 
     pane.setVisible(false); 
     Container cnt = f.getLayeredPane(); 
     BorderLayout bl = new BorderLayout(); 
     bl.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE); 
     cnt.setLayout(bl); 
     if (center) { 
      cnt.addComponent(BorderLayout.CENTER, new InfiniteProgress()); 
     } else { 
      Container top = new Container(); 
      BorderLayout bl1 = new BorderLayout(); 
      bl1.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE); 
      top.setLayout(bl1); 
      top.addComponent(BorderLayout.CENTER, new InfiniteProgress()); 

      cnt.addComponent(BorderLayout.NORTH, top); 
     } 
    } 

    private static void removeWaitingProgress(Form f) { 
     removeWaitingProgress(f, f.getContentPane()); 
    } 

    private static void removeWaitingProgress(Form f, Container pane) { 
     Container cnt = f.getLayeredPane(); 
     cnt.removeAll(); 
     pane.setVisible(true); 
    } 

    /** 
    * This method builds a UI Entry dynamically from a data Map object. 
    */ 
    @SuppressWarnings("rawtypes") 
    private static Component createEntry(Map data, final int index) { 
     final Container cnt = new Container(new BorderLayout()); 
     cnt.setUIID("MultiButton"); 
     Button icon = new Button(); 
     icon.setUIID("Label"); 
     //take the time and use it as the identifier of the image 
     String time = (String) data.get("date_taken"); 
     String link = (String) ((Map) data.get("media")).get("m"); 

     EncodedImage im = (EncodedImage) res.getImage("flickr.png"); 
     final URLImage image = URLImage.createToStorage(im, time, link); 
     icon.setIcon(image); 
     icon.setName("ImageButton" + index); 
     icon.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent evt) { 

       Dialog d = new Dialog(); 
       //d.setDialogUIID("Container");     
       d.setLayout(new BorderLayout()); 
       Label l = new Label(image); 
       l.setUIID("ImagePop"); 
       d.add(BorderLayout.CENTER, l); 
       d.setDisposeWhenPointerOutOfBounds(true); 
       d.setTransitionInAnimator(new BubbleTransition(300, "ImageButton" + index)); 
       d.setTransitionOutAnimator(new BubbleTransition(300, "ImageButton" + index)); 
       d.show(); 
      } 
     }); 

     cnt.addComponent(BorderLayout.WEST, icon); 

     Container center = new Container(new BorderLayout()); 

     Label des = new Label((String) data.get("title")); 
     des.setUIID("MultiLine1"); 
     center.addComponent(BorderLayout.NORTH, des); 
     Label author = new Label((String) data.get("author")); 
     author.setUIID("MultiLine2"); 
     center.addComponent(BorderLayout.SOUTH, author); 

     cnt.addComponent(BorderLayout.CENTER, center); 
     return cnt; 
    } 
} 
+0

您可以分享您的代碼以解釋您在使用webservice進行的操作嗎?謝謝 –

+0

你可以再看看這個問題嗎?我找到了它通過theme.res文件發生的原因。但我不知道什麼是組件URLImage @TimWeber – MathaN

回答

2

兩種解決方案:

這裏嘗試刪除最後一個「」參數:
final URLImage image = URLImage.createToStorage(im, time, link, null);
它將不使用圖像適配器參數,不拋出異常。
(ImageAdapter是有用的加載它後,調整圖片大小,如果不帶參數,ImageAdapter.SCALE_TO_FILL的使用和下載的圖片將被調整,以填補你的圖像佔位符。)這裏解釋:javadoc

或者:

您是否已經導入了指定的im? (用於你的URLImage.createToStorage)
這張圖片是你的urlimage下載的佔位符。嘗試在theme.res中導入多圖像,然後在您的代碼中創建如下代碼:

try{ 
     Resources res = Resources.openLayered("/theme"); 
     Image im = res.getImage("myImportedImage"); 
     final URLImage image = URLImage.createToStorage(im, time, link); 
    } 
catch(Exception e){ 
     e.printStackTrace(); 
    } 
+0

我已經刪除了null,現在它在佔位符中給出空指針異常。感謝您的答覆。我想這個錯誤是由theme.res文件發生的。因爲如果我從閃爍演示覆制theme.res文件,我沒有遇到問題@Timweber – MathaN

+0

我們是否需要編輯theme.res? – MathaN

+0

所以第二種解決方案是正確的。事實上,你的圖像被加載到theme.res中。如果您沒有使用設計工具導入圖像(右鍵單擊theme.res>打開設計器),您的佔位符將爲空。 所以正確的方法@MathaN是: 1.導入您的圖像在您的theme.res 2.定義您的佔位符與您導入的圖像 –