使用代号一个应用程序从Storage初始化文本字段值

时间:2014-05-30 13:17:05

标签: java swing codenameone

我是Codename One的新手。至于3天新..

我在主窗体中有一个配置文件容器。我有输入文本字段。当用户键入数据并单击“保存”按钮时,配置文件数据将保存到“存储”。这很有效。

现在 - 当用户离开应用程序并返回到个人资料页面时,如果他们已经输入了内容,我希望个人资料数据位于文本字段中。

所以我有一个像这样的beforeMain方法:

   @Override
   protected void beforeMain(Form f) {
    // Get stored profile data here

    Storage inappStore = Storage.getInstance();

    Util.register("Profile", Profile.class);

    if (inappStore.readObject("profile") != null) {
        Profile userProfile = (Profile) inappStore.readObject("profile");

        findFirstnameTextField().setText(userProfile.getFirstname());

    }
  }

我收到此错误:

  java.lang.reflect.InvocationTargetException
   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.codename1.impl.javase.Executor$1.run(Executor.java:95)
   at com.codename1.ui.Display.processSerialCalls(Display.java:1075)
   at com.codename1.ui.Display.mainEDTLoop(Display.java:897)
   at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
   at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Caused by: java.lang.NullPointerException
at com.codename1.ui.util.UIBuilder.findByName(UIBuilder.java:578)
at generated.StateMachineBase.findFirstnameTextField(StateMachineBase.java:250)
at userclasses.StateMachine.beforeMain(StateMachine.java:51)
at generated.StateMachineBase.beforeShow(StateMachineBase.java:406)
at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2436)
at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2483)
at generated.StateMachineBase.startApp(StateMachineBase.java:62)
at generated.StateMachineBase.<init>(StateMachineBase.java:31)
at generated.StateMachineBase.<init>(StateMachineBase.java:110)
at userclasses.StateMachine.<init>(StateMachine.java:24)
at za.co.vine.communicatorFeature.CommunicatorApplication.start(CommunicatorApplication.java:35)
... 9 more

如何初始化字段值?

1 个答案:

答案 0 :(得分:0)

使用findFirstnameTextField(f)注意f参数。