在codenameone中调用web服务时出现空指针异常
我是一名Android开发人员,目前我正在尝试开发跨平台应用程序,我使用了flicker演示代码,我刚刚从演示中复制了所有图像和所有java文件,除了web服务调用外,一切正常,调用web服务时出现空指针异常 这是web服务链接: 以下代码发生异常在codenameone中调用web服务时出现空指针异常,codenameone,Codenameone,我是一名Android开发人员,目前我正在尝试开发跨平台应用程序,我使用了flicker演示代码,我刚刚从演示中复制了所有图像和所有java文件,除了web服务调用外,一切正常,调用web服务时出现空指针异常 这是web服务链接: 以下代码发生异常 final URLImage image = URLImage.createToStorage(im, time, link, null); 如果我从flicker演示项目复制theme.res,我就不会遇到这个异常 我发现它是按主题发生的 但
final URLImage image = URLImage.createToStorage(im, time, link, null);
如果我从flicker演示项目复制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)
键值应为空!
java.lang.NullPointerException
在com.codename1.ui.urlmeage.(urlmeage.java:154)
在com.codename1.ui.URLImage.createToStorage(URLImage.java:357)上
在com.agarangroup.flicker.Flickerdemo.createEntry(Flickerdemo.java:264)上
在com.agarangroup.flicker.Flickerdemo.access$3(Flickerdemo.java:254)
在com.agarangroup.flickle.flickledemo$3$1.run(flickledemo.java:204)
位于com.codename1.ui.Display.processSerialCalls(Display.java:1150)
在com.codename1.ui.Display.edtlopimpl(Display.java:1094)上
位于com.codename1.ui.Display.mainEDTLoop(Display.java:995)
位于com.codename1.ui.runnablerrapper.run(runnablerrapper.java:120)
位于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;
}
}
package com.agarangroup.flicker;
导入java.io.IOException;
导入java.util.List;
导入java.util.Map;
导入com.codename1.components.InfiniteProgress;
导入com.codename1.media.media;
导入com.codename1.media.MediaManager;
导入com.codename1.ui.Button;
导入com.codename1.ui.Command;
导入com.codename1.ui.Component;
导入com.codename1.ui.Container;
导入com.codename1.ui.Dialog;
导入com.codename1.ui.Display;
导入com.codename1.ui.EncodedImage;
导入com.codename1.ui.FontImage;
导入com.codename1.ui.Form;
导入com.codename1.ui.Image;
导入com.codename1.ui.Label;
导入com.codename1.ui.TextArea;
导入com.codename1.ui.Toolbar;
导入com.codename1.ui.URLImage;
导入com.codename1.ui.animations.bubblettransition;
导入com.codename1.ui.events.ActionEvent;
导入com.codename1.ui.events.ActionListener;
导入com.codename1.ui.layouts.BorderLayout;
导入com.codename1.ui.layouts.BoxLayout;
导入com.codename1.ui.plaf.UIManager;
导入com.codename1.ui.util.Resources;
公共类闪烁演示{
私有形式电流;
私有静态资源;
公共void init(对象上下文){
res=UIManager.initFirstTheme(“/themes”);
//仅限Pro功能,如果您有Pro订阅,请取消注释
//Log.bindCrashProtection(true);
}
公开作废开始(){
如果(当前!=null){
current.show();
返回;
}
Form main=createMainForm();
main.show();
}
@抑制警告(“弃用”)
私有表单createMainForm(){
表单main=新表单(“Flickr标签”);
main.setLayout(新的BorderLayout());
工具栏=新工具栏();
main.setToolBar(bar);
addCommandsToToolbar(条形图);
TextArea desc=新建TextArea();
desc.setText(“这是一个Flickr标记演示,演示使用工具栏来排列表单命令。\n\n”
+选择“猫”可查看标记为“猫”的最新照片\n\n
+选择“Dogs”可查看标记为“Dogs”的最新照片\n\n
+“选择\“搜索\”以输入您自己的标签进行搜索。”);
desc.setEditable(假);
main.addComponent(BorderLayout.CENTER,desc);
回水总管;
}
专用void addCommandsToToolbar(工具栏工具){
//TODO自动生成的方法存根
工具.addCommandToSideMenu(新命令(“主”){
@凌驾
已执行的公共无效操作(操作事件evt){
Form main=createMainForm();
main.show();
}
});
工具.addCommandToSideMenu(新命令(“Cats”){
@抑制警告(“弃用”)
@凌驾
已执行的公共无效操作(操作事件evt){
最终形式猫=新形式(“猫”);
cats.setLayout(新的BoxLayout(BoxLayout.Y_轴));
猫。setScrollableY(真);
最终自定义工具栏=新的自定义工具栏(true);
cats.getContentPane().addScrollListener(条);
cats.setToolBar(bar);
addCommandsToToolbar(条形图);
图像图标=FontImage.createMaterial(FontImage.MATERIAL_刷新,UIManager.getInstance().getComponentStyle(“标题命令”);
addCommandToRightBar(新命令(“,图标){
@凌驾
已执行的公共无效操作(操作事件evt){
Display.getInstance().callSerially(new Runnable()){
公开募捐{
updateScreenFromNetwork(cats,“cat”);
cats.revalidate();
}
});
}
});
bar.addCommandToOverflowMenu(新命令(“清除”){
@凌驾
已执行的公共无效操作(操作事件evt){
容器cnt=(容器)cats.getContentPane();
cnt.removeAll();
//加回大愤怒猫的形象
试一试{
try{
Resources res = Resources.openLayered("/theme");
Image im = res.getImage("myImportedImage");
final URLImage image = URLImage.createToStorage(im, time, link);
}
catch(Exception e){
e.printStackTrace();
}