0

Alert credits is not getting visible , whats wrong in the code ?? I made a list named gameMenu , and I want to access different functions or Alerts in Click of every different list items. For eg: If I want to click on Credits item from the list then it should Alert me credits alert. I tried a lot but it is not getting pop.

package project2;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;


/**
 * @author Abhishek
 */
public class Midlet extends MIDlet implements CommandListener{

public Form form;
Display disp;
String pname;
public Command ok,exit;   
public Alert printText; 
public TextField txt;
public List gameMenu = new List("Choose one", Choice.IMPLICIT);



    public void startApp() {
         disp = Display.getDisplay(this);
         ok = new Command("OK", Command.SCREEN, 1);
         exit = new Command("EXIT", Command.SCREEN, 2);
        form = new Form("Player Name? ");
        txt=new TextField("Name: ", "", 32, TextField.ANY);
        form.append(txt);
     //  System.out.println(pname);
     txt.getString();
        form.addCommand(ok);
        form.addCommand(exit);
   form.setCommandListener(this);

        disp.setCurrent(form);

    }
     public void menu() {

     gameMenu.append("Start Game", null);
     gameMenu.append("Credits", null);
     gameMenu.append("highScore", null);

     disp.setCurrent(gameMenu);
   }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
    //   String index = gameMenu.getString(gameMenu.getSelectedIndex());
        String lbl=c.getLabel();
        if(lbl.equals("OK")&& d==form)
        {
             printText= new Alert("Welcome! To Road Fighter Game ! :) ",txt.getString(), null , null);
             printText.setTimeout(Alert.FOREVER);
                printText.addCommand(ok);
                printText.setCommandListener(this);
             disp.setCurrent(printText);

        }else if(lbl.equals("OK") && d==printText)
        {
            menu();
            gameMenu.addCommand(ok);
      gameMenu.addCommand(exit);
      gameMenu.setCommandListener(this);
     // int in=gameMenu.getSelectedIndex();
      if(lbl.equals("OK"))
      {
          if(gameMenu.getSelectedIndex() == 1) {
                Alert credits=new Alert("Credits! ", "ROAD FIGHTER GAME \n by \n Abhishek Sharma", null, AlertType.INFO);
              credits.setTimeout(Alert.FOREVER);
              disp.setCurrent(credits);
            }

       }

        }else if(lbl.equals("EXIT"))
        {
            destroyApp(true);
        }/*else if(List.SELECT_COMMAND==c && d==gameMenu)
        {
            if (gameMenu.getSelectedIndex()==1) {
                Alert credits=new Alert("Credits! ", "ROAD FIGHTER GAME \n by \n Abhishek Sharma", null, AlertType.INFO);
                credits.setTimeout(Alert.FOREVER);
                disp.setCurrent(credits);
            }
        } */

        }



    }
  • Since `Displayable` and `Form` are objects, you cannot compare them using an equals-sign. You need to use `.equals()` like you do on the `Label`. `if (lbl.equals("OK") && d.equals(form)) ...` and likewise with the `printText` object. Might help. – mr_lou Apr 29 '14 at 16:00
  • thank you ! I am getting one more problem can you please solve than too. Plz visit to my profile . – Abhishek Sharma Apr 29 '14 at 16:43

0 Answers0