GET - 为什么这段历史不起作用?

时间:2010-11-21 20:01:14

标签: gwt history gwt-history

我一直在尝试GWT的历史。我觉得这个approch非常适合实现。不幸的是,我的代码无法正常工作。

这是我的实际代码:

public class PageLoader implements EntryPoint, ValueChangeHandler<String> {
    private FlowPanel pageloader;
    private GWTServiceAsync rpcService;

    public void onModuleLoad() {
        pageloader=new FlowPanel();
        rpcService=GWT.create(GWTService.class);
        pageloader.add(new HomepageContext(rpcService));
        RootPanel.get().add(pageloader);

        History.addValueChangeHandler(this);
        if(!History.getToken().isEmpty()){
            changePage(History.getToken());
        }
    }

    public void onValueChange(ValueChangeEvent event) {
        changePage(History.getToken());
    }

    public void changePage(String token) {
        if(History.getToken().equals("apartment")) {
            pageloader.clear();
            pageloader.add(new ApartmentContext(rpcService));
        } else {
            pageloader.clear();
            pageloader.add(new HomepageContext(rpcService));
        }
    }
}

############################################################

public class ApartmentContext extends Index {
    public ApartmentContext(GWTServiceAsync rpcService) {
        super(rpcService);
        this.getContent().add(new Apartment());
    }
}    

public class Apartment extends FlowPanel {
    // somethings
}

############################################################

public class Index extends FlowPanel {
    private Center center;
    public Index(GWTServiceAsync rpcService) {
        this.center=new Center(rpcService);
        this.add(center);
   }
   public FlowPanel getContent() {
       return center.getContent();
   }
} 

############################################################

public class Center extends FlowPanel {
    private Menu menu;
    private Content content;

    public Center(GWTServiceAsync rpcService) {
       this.content=new Content();
       this.menu=new Menu(rpcService, this);

       this.add(menu);
       this.add(content);
    }

    public Content getContent() {
        return this.content;
    }
}  

当我尝试加载#apartment时,我得到:

01:19:45,112 [ERROR] Uncaught exception escaped
java.lang.NullPointerException: null    
at org.sinfonet.client.apartment.Apartment.<init>(Apartment.java:24)    
at org.sinfonet.client.context.ApartmentContext.<init>(ApartmentContext.java:14)    
at org.sinfonet.client.PageLoader.changePage(PageLoader.java:36)    
at org.sinfonet.client.PageLoader.onValueChange(PageLoader.java:30)     
at com.google.gwt.event.logical.shared.ValueChangeEvent.dispatch(ValueChangeEvent.java:128)     
at com.google.gwt.event.logical.shared.ValueChangeEvent.dispatch(ValueChangeEvent.java:1)   
at com.google.gwt.event.shared.HandlerManager$HandlerRegistry.fireEvent(HandlerManager.java:65)     
at com.google.gwt.event.shared.HandlerManager$HandlerRegistry.access$1(HandlerManager.java:53)  
at com.google.gwt.event.shared.HandlerManager.fireEvent(HandlerManager.java:178)    
at com.google.gwt.user.client.impl.HistoryImpl.fireEvent(HistoryImpl.java:74)   
at com.google.gwt.event.logical.shared.ValueChangeEvent.fire(ValueChangeEvent.java:43)  
at com.google.gwt.user.client.impl.HistoryImpl.fireHistoryChangedImpl(HistoryImpl.java:81)  
at com.google.gwt.user.client.impl.HistoryImpl.newItem(HistoryImpl.java:118)    
at com.google.gwt.user.client.History.newItem(History.java:173)     
at com.google.gwt.user.client.History.newItem(History.java:156)     
at com.google.gwt.user.client.ui.Hyperlink.onBrowserEvent(Hyperlink.java:163)   
at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1307)  
at com.google.gwt.user.client.DOM.dispatchEvent(DOM.java:1263)  
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    
at java.lang.reflect.Method.invoke(Method.java:597)     
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)    
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)   
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)    
at com.google.gwt.dev.shell.BrowserChannel.reactToMessagesWhileWaitingForReturn(BrowserChannel.java:1714)   
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:165)    
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)    
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)  
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)    
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)   
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)    
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)   
at sun.reflect.GeneratedMethodAccessor131.invoke(Unknown Source)    
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    
at java.lang.reflect.Method.invoke(Method.java:597)     
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)    
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)   
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)    
at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1669)    
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)   
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)     
at java.lang.Thread.run(Thread.java:619)

1 - 如果我很清楚,当我使用History.newItem()时,我会调用onValueChange()吗?那为什么不起作用?

2 - 班级历史是静态的?所以我不必随时传递它;我可以在申请的任何竞赛中给它打电话吗?

1 个答案:

答案 0 :(得分:1)

在你的changePage()方法上,尝试使用参数中的标记变量:

public void changePage(String token)
{
  if(“apartment”.equals(token)){...}
  否则{...}
}

没有注意到你有异常......这个异常发生在哪里? (代码行..)