我是Android开发人员,目前我正在尝试开发跨平台应用程序,我使用了闪烁演示代码,我只是从演示中复制了所有图像和所有java文件,一切正常,除了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);
如果我从闪烁演示项目中复制了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 :(得分:2)
两个解决方案:
尝试删除最后一个&#39; null &#39;参数在这里:
final URLImage image = URLImage.createToStorage(im, time, link, null);
它不会使用Image Adapter参数,也不会抛出异常。
(ImageAdapter用于在加载图片后调整图片大小。如果没有参数,则使用ImageAdapter.SCALE_TO_FILL,并调整下载的图片大小以填充占位符图片)此处说明:javadoc
或:
您是否导入了指定的 im ? (用于您的URLImage.createToStorage)
下载您的urlimage时,此Image是占位符。尝试在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();
}