×

Discussion Board

Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Exclamation Problem with Left key of mobile

    Hi.
    I'm working on a project, and I implemented a menu which I called it in the Midlet class and I implemented a list class that will display on the screen when I click an item of the menu,after that when I use left click for an item of the List class which is type of Canvas, a Canvas class named Fd must display,I called List class in the Midlet class too but I did'nt call Fd class in the Midlet!I wrote a code to display the Canvas like bellow but when I run the program,an error occuers in the code I wrote and it doesn't show the Fd class,
    Keycode of Leftkey I used is true,cause when I wrote System.Out.Print();
    it worked!could anybody help me to correct the code please?

    protected void keyPressed(int keyCode)
    {
    int gameAction = this.getGameAction(keyCode);
    switch(gameAction)
    {

    case Canvas.LEFT:
    Display.getDisplay(midlet).setCurrent( new Fd(this,midlet));
    break;}
    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with Left key of mobile

    Welcome to Forum Nokia!

    What error do you get? Is it when you compile, or when you run the application?

    Graham.

  3. #3
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: Problem with Left key of mobile

    thanks for your reply,it is when I run the application, the error is java.lang.NullPointerException at Display.getDisplay(midlet).setCurrent( new Fd(this,midlet));

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with Left key of mobile

    That sounds like "midlet" is null. Try adding:

    Code:
    System.out.println(midlet == null ? "Graham was right" : "Guess again, Graham!!  You got it wrong!!");
    Graham.

  5. #5
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: Problem with Left key of mobile

    It printed Graham was right, so what is your suggestion?
    Elenora

  6. #6
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with Left key of mobile

    The variable "midlet" is null. You need to assign a value to it somewhere.

    Graham.

  7. #7
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: Problem with Left key of mobile

    Did you mean I assign a value to midlet in List Class? I did it in constructor of my List class like below but it did'nt work!

    ListMIDlet midlet;
    public List(String[] strings,ListMIDlet midlet)
    {
    this.midlet=midlet;
    }
    Last edited by elenora; 2011-02-19 at 10:59.

  8. #8
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: Problem with Left key of mobile

    The Fd class which I implemented is like below:

    public class Fd extends Canvas {
    private int width;
    private int height;
    List s;
    final ListMIDlet midlet;
    public Fd(List s,ListMIDlet midlet)
    {
    this.s=s;
    this.midlet = midlet;

    width = this.getWidth();
    height = this.getHeight();
    // Display.getDisplay(midlet).setCurrent(this);
    }

    protected void paint(Graphics g)
    {
    g.setColor(0xbfd4f6);
    g.fillRect(0, 0, width, height);
    }
    }

  9. #9
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with Left key of mobile

    The problem is not in the Fd class.

    Track the value you think is in "midlet" back to where it comes from. Find out why it is null. For example:

    Code:
    public List(String[] strings,ListMIDlet midlet)
    {
        System.out.println("List constructor: midlet is " + (midlet == null : "null" : "not null"));
        this.midlet=midlet;
    }
    Graham.

  10. #10
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: Problem with Left key of mobile

    Thank you very much Mr Graham,I solved my problem!

  11. #11
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Exclamation Re: Problem with Left key of mobile

    Dear sir,
    I have a problem in my List class again which is type of Canvas, that is I want to use small pictures like checkbox near list’s items and each picture must changed when I click on corresponding item of the List,I don’t know how to do it! Could you help me what I have to do?
    Last edited by elenora; 2011-02-19 at 15:46.

  12. #12
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with Left key of mobile

    Well, I presume you have some information in your List class to hold the "checked" state of each item, something like:

    Code:
    private boolean[] checked;
    Then in your paint(), I'm guessing you use this to decide what to paint:

    Code:
    protected void paint(Graphics g) {
        // ...some code...
    
        // paint the items in the list
        for (int i = 0; i < listSize; i++) {
            // select and draw the correct image...
            Image box;
            if (checked[i]) {
                box = checkedImage;
            } else {
                box = uncheckedImage;
            }
            g.drawImage(box, x, y, Graphics.LEFT | Graphics.TOP);
    
            // draw the text part of the item here...
    
            // advance "x" for the next iteration
            x += itemHeight;
        }
    
        // ...some more code...
    }
    And, I guess you have some code in pointerPressed() to work out which item was touched. In which case, you need to add a couple of lines of code...

    Code:
    protected void pointerPressed(int x, int y) {
        int itemNumber = getItemNumber(x, y);
    
        // you add:
        checked[i] = !checked[i];
        repaint();
    }
    Does that help?

    Graham.

  13. #13
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Question Re: Problem with Left key of mobile

    Thanks for your reply! I initialized list’s items in the Midlet class like below and then pass them into the function which I called in the Midlet, but now I don’t know how to initialize images?

    public void startApp()
    {
    try {
    img = Image.createImage("/PNG/arrowLeft.png");
    imgch = Image.createImage("/PNG/check.png");
    imgun = Image.createImage("/PNG/uncheck.png");
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    //
    String[] texts = new String[] {
    "a",
    "b",
    "c",
    "d",
    "f"
    };

    list = new List(texts,font, this);
    // list.item_image=new Image[5];
    Display.getDisplay(this).setCurrent(list);
    }
    Last edited by elenora; 2011-02-19 at 20:11.

  14. #14
    Regular Contributor
    Join Date
    Feb 2011
    Posts
    73

    Re: Problem with Left key of mobile

    this is my paint method in List class, where should I draw the Image?:

    public List(String[] strings, CBitMap font,ListMIDlet midlet)
    {
    this.midlet=midlet;
    if(font != null && strings != null)
    {
    int itemNum = strings.length;
    this.items = new CBitMapViewer[itemNum];
    //create for example English viewers from strings.
    for(int i = 0; i < itemNum; i++)
    {
    String shaped = EJStringConvertor.getShapedUTF(strings[i]);
    this.items[i] = font.getViewer(shaped);
    this.items[i].setHorizontalOrientation(Canvas.RIGHT);
    }

    //initializing list variables.
    if(itemNum > 0)
    {
    this.itemHeight = this.items[0].getHeight();
    this.currentItem = 0;
    this.offset = 0;
    }
    repaint();
    }
    }

    protected void paint(Graphics g)
    {int l;
    int m;
    int k;
    int j;
    int p;
    int h;
    int canvasHeight = this.getHeight();
    int canvasWidth = this.getWidth();
    l = canvasHeight;
    m = canvasWidth / 6;
    k = canvasHeight;
    j = canvasWidth / 12;
    p = 2;
    h = (canvasWidth - (j + m) - 8 * p) /8;
    h--;
    int i1 = m + p + 1;
    //clear screen.
    int listWidth = canvasWidth - MARGIN_LEFT - MARGIN_RIGHT;
    int listHeight = canvasHeight - MARGIN_TOP - MARGIN_BOTTOM;
    g.setColor(0xff00ff);
    g.setClip(0, 0, canvasWidth, canvasHeight);
    g.fillRect(0, 0, canvasWidth, canvasHeight);
    g.clipRect(
    MARGIN_LEFT,
    MARGIN_RIGHT,
    listWidth,
    listHeight);

    if(this.items != null)
    {
    //draw items on the screen.
    int y = - this.offset; // variable y is width of current item of the list
    for(int i = 0; i < this.items.length;i++)
    {

    if(i == this.currentItem)
    {
    //draw focused cursor
    g.setColor(0xfff0a0);
    g.fillRect(MARGIN_LEFT,
    y + MARGIN_TOP,
    listWidth,
    this.itemHeight);
    // g.drawImage(scaleImage(item_image[i], h / 2, h / 2), canvasHeight-canvasHeight/6, i1,0);
    }
    items[i].paint(
    canvasWidth - MARGIN_RIGHT,
    y + MARGIN_TOP,
    g);
    y += this.itemHeight + ITEM_MARGIN;
    // g.drawImage(scaleImage(item_image[i], h / 2, h / 2), canvasHeight-canvasHeight/6, i1,0);
    }
    }
    }

  15. #15
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Problem with Left key of mobile

    I don't know what your CBitMap and CBitMapViewer classes are (they sound like MFC classes!!)... but you should paint your checked/unchecked image in side the for() loop in the paint() method.

    Graham.

Similar Threads

  1. Replies: 18
    Last Post: 2010-03-29, 21:24
  2. key for 2,4,6,8 with Up,Left,Right,Down
    By sunitha.m13 in forum Symbian
    Replies: 16
    Last Post: 2009-10-30, 12:50
  3. APPLICATION MENU with left selection key???
    By sagars in forum Symbian
    Replies: 13
    Last Post: 2008-07-24, 07:34
  4. Replies: 2
    Last Post: 2005-09-03, 18:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×