2

我试图通过为我的应用程序
表单选项使用多个类来使我的代码更整洁。目前,我在尝试setCurrent.

这是我的主类,当我调用另一个类时,错误在我的命令侦听器中开始。

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class CalFrontEnd extends MIDlet implements CommandListener {

  private Display display;
  private List list = new List("Please Select a Option", List.IMPLICIT);
  private List Blist = new List("Please Select a Browsing Option", List.IMPLICIT);
  private Command select = new Command("Select", Command.SCREEN, 1);
  private Command exit = new Command("Exit", Command.EXIT, 2);
  private Command save = new Command("Save,", Command.SCREEN, 2);
  private DateField calendar;
  Alert alert;

  //
  //
  //
  public CalFrontEnd() {
    display = Display.getDisplay(this);   

    list.append("Select Date", null);
    list.append("Add Events", null);
    list.append("Remove Events", null);
    list.append("Browse Events", null);
    list.addCommand(select);
    list.addCommand(exit);
    list.setCommandListener(this);

  }


  //
  //Start Application Method
  //
  public void startApp() {
    display.setCurrent(list);
  }


  //
  //Pause Application Method
  //
  public void pauseApp() {
  }


  //
  //Destroy Application Method
  //
  public void destroyApp(boolean unconditional) {
  }


  //
  //Method creates form which contains calendar
  //
  /*public void selectDate()
  {
      calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
      Form cform = new Form("Calendar");
      cform.append(calendar);
      cform.addCommand(exit);
      display.setCurrent(cform);
  }*/


  //
  //Method creates form which contains adding events
  //
  public void AddEvents()
  {
      TextBox aeText = new TextBox("Add Event","", 256, 0);
      display.setCurrent(aeText);
  }


  //
  //Method creates form which contains removing events
  //
  public void RemoveEvents()
  {

      Form reform = new Form("Remove Event");
      reform.append(calendar);
      display.setCurrent(reform);
  }


  //
  //Method creates form which contains removing events
  //
  public void BrowseEvents()
  {
      Blist.append("Monthly", null);
      Blist.append("Daily", null);
      Blist.addCommand(select);
      Blist.addCommand(exit);
      Blist.setCommandListener(this);
      display.setCurrent(Blist);
  }


  //
  //but it's better practice to make each form a different class extending CommandListener and it's own commandAction. And leave the display public static in MIDlet class
  //...
  public void commandAction(Command command, Displayable displayable) {
      if (displayable == list) {
          if (command == List.SELECT_COMMAND) {
              switch (list.getSelectedIndex()) {
                  case 0: // select date
                      SelectDate.BuildCalendar(); //Error Here
                      break;
                  case 1: //add events
                      AddEvents();
                      break;
              }
          } else if (command == exit) {
             destroyApp(false);
             notifyDestroyed();
          }
      }         
  }
}

这是被调用的类。

public class SelectDate
{

    private static DateField calendar;
    private static Form form = new Form("derb");
    private static Command select = new Command("Select", Command.SCREEN, 1);
    private static Command exit = new Command("Exit", Command.EXIT, 2);
    private static Command save = new Command("Save,", Command.SCREEN, 2);
    private static Display display;


    public static void BuildCalendar()
    {
        calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
        form.append(calendar);
        form.addCommand(exit);
        display.setCurrent(form);
    }  
}
4

1 回答 1

1

NullPointerException 发生是因为display在 SelectDate 类中为空。

要解决这个问题,您可以例如从那里删除它,而是添加到方法参数:

// ...
public static void BuildCalendar(Display display) // added parameter

然后,当您从 调用上述方法时CalFrontEnd,传递display那里的实例:

                      // ...
                      SelectDate.BuildCalendar(display); //Error will go away
于 2012-05-03T15:58:02.850 回答