***我试图显示一个填充了图像和文本的列表,当我从UI构建器渲染res文件时,我得到空指针异常。以下是代码:
@Override
protected void beforeMain(Form f){
super.beforeMain(f);
final List list = findList(f);
Component selected = createContainer(fetchResourceFile(), "Renderer");
Component unselected = createContainer(fetchResourceFile(), "Renderer");
System.out.println(selected.getName());
list.setRenderer(new GenericListCellRenderer(selected, unselected) {
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
if ((index + 1) >= list.size()) {
fetchMore(list);
}
return super.getListCellRendererComponent(list, value, index, isSelected);
}
private void fetchMore(final List list) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
NetworkManager networkManager = NetworkManager.getInstance();
networkManager.start();
networkManager.addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent n = (NetworkEvent) evt;
n.getError().printStackTrace();
}});
ConnectionRequest request = new ConnectionRequest() {
String array[] ;
String images[];
//StringBuffer sb = new StringBuffer();
Hashtable h;
Vector tweets;
// @Override
protected void postResponse() {
//cmp.setModel(new com.codename1.ui.list.DefaultListModel(array));
}
protected void readResponse(InputStream input) throws IOException {
Result result = Result.fromContent(input, Result.XML);
array = result.getAsStringArray("/music[1]//thumb_url");
int count = list.size();
for (int i = 0; i < array.length; i++) {
// Hashtable tweet = (Hashtable)tweets.elementAt(i);
list.addItem(array[i]);
addAvatar(list, array[i], count + i);
}
System.out.println(""+array.length);
}
private void addAvatar(List list, String array, int i) {
// TODO Auto-generated method stub
//String url = (String)array.get("profile_image_url");
//String user = (String)array.get("from_user");
// if (array == null || array.startsWith("http:") == false) {
// ImageDownloadService doesn't support HTTPS at moment
// return;
//}
ImageDownloadService ids = new ImageDownloadService(array, list, i, "avatar");
ids.setDuplicateSupported(true);
NetworkManager.getInstance().addToQueue(ids);
ImageDownloadService.createImageToStorage(array, list, i, "avatar", "-avatar",
new Dimension(48, 48));
}
protected void handleException(Exception err) {
//An error occured - show a message:
Dialog.show("Alert", "Are you connected to the internet? Check your connection", "Ok", null);
}
};
request.setUrl("http://api.androidhive.info/music/music.xml"); //servlet calling
request.setPost(false);
//infiniteProgress prog=
InfiniteProgress prog=new InfiniteProgress();
Dialog dlg=prog.showInifiniteBlocking();
request.setDisposeOnCompletion(dlg);
networkManager.addToQueueAndWait(request);
}
}); }
以下是异常通过
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)下的sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)的java.lang.reflect.InvocationTargetException,位于java.lang.reflect的sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)。在com.codename1.impl.javase.Executor $ 1.run(Executor.java:95)的com.codename1.ui.Display.processSerialCalls(Display.java:1075)的com.codename1.ui上的Method.invoke(未知来源) com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)中的.Display.mainEDTLoop(Display.java:897)at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)引起: java.lang.NullPointerException at userclasses.StateMachine.beforeMain(StateMachine.java:117) at generated.StateMachineBase.beforeShow(StateMachineBase.java:218)at com.codename1.ui.util.UIBuilder.showForm(UIBuilder) .java:2436)at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2 483)在generate.StateMachineBase(StateMachineBase.java:31)的generated.StateMachineBase.startApp(StateMachineBase.java:57)at user.StateMachineBase。(StateMachineBase.java:100)at userclasses.StateMachine。(StateMachine.java:38 )在com.prime.cnna.MyApplication.start(MyApplication.java:62)
空指针跟随行的异常
list.setRenderer(new GenericListCellRenderer(selected,unselected){
}); * 的
答案 0 :(得分:0)
在不突出显示实际行117的情况下指定异常并不是很有帮助。
您正在从渲染器执行网络请求,这可能不是一个好方法,每分钟会被调用数千次。您应该使用该模型来实现延迟数据提取,例如https://code.google.com/p/codenameone/source/browse/trunk/CodenameOne/src/com/codename1/cloud/CloudListModel.java