黑莓Tab栏控制

时间:2012-04-16 13:12:24

标签: blackberry

我想在我的应用程序中使用标签栏。我在黑莓手机中尝试了它的示例代码,但我想在屏幕的底部。我尝试了这段代码,但它给了我未被捕获的运行时异常。为什么这样呢?这个代码中的问题是什么?

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.image.*;
import net.rim.device.api.ui.toolbar.*;
import net.rim.device.api.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.command.*;

public class ToolbarDemo extends UiApplication
{
    public static void main(String[] args)
    {

        ToolbarDemo theApp = new ToolbarDemo();       
        theApp.enterEventDispatcher();
    }

    public ToolbarDemo()
    {        
        // Push a screen onto the UI stack for rendering.
        pushScreen(new ToolbarDemoScreen());
    }

    private final static class ToolbarDemoScreen extends MainScreen
    {
        public ToolbarDemoScreen()
        {              
            if (ToolbarManager.isToolbarSupported()) 
            {
                setTitle("Toolbar Demo");

                ToolbarManager manager = new ToolbarManager();
                setToolbar(manager);

                try 
                {
                    Bitmap myBitmap = Bitmap.getBitmapResource("myImg.jpg");
                    Image myImage = ImageFactory.createImage(myBitmap);

                    /*
                     * To create more buttons, Repeat the following lines 
                     * up until manager.add() 
                     */
                    ToolbarButtonField button1 = new ToolbarButtonField(myImage, new StringProvider("butn1"));
                    button1.setCommandContext(new Object()
                    {
                        public String toString()
                        {
                            return "Button1"; 
                        }          
                    });

                    button1.setCommand(new Command(new CommandHandler()
                    {         
                        public void execute(ReadOnlyCommandMetadata metadata, Object context)
                        {
                            Dialog.alert("Executing command for " + context.toString());
                        }           
                    }));

                    manager.add(new ToolbarSpacer(0));
                    manager.add(button1);
                }
                catch (Exception e)
                {
                    System.out.println(e.getMessage());
                }
            } 
            else 
            {
                Dialog.alert("The Toolbar is not supported on this device.");
            }
        }     
    }
}

2 个答案:

答案 0 :(得分:1)

如果您收到例外Uncaught Exception: pushModalScreen called by a non-event thread,则可以尝试使用以下代码段来显示提醒。

UiApplication.getUiApplication().invokeLater(new Runnable() {
    public void run() {
        Dialog.alert("The Toolbar is not supported on this device.");
    }
});

请查看此问题,“pushModalScreen called by a non-event thread” thrown on event thread

答案 1 :(得分:0)

试试此代码

EncodedImage e1 = EncodedImage.getEncodedImageResource("home.png");
EncodedImage e2 = EncodedImage.getEncodedImageResource("map.png");
EncodedImage e3 = EncodedImage.getEncodedImageResource("members.png");
EncodedImage e4 = EncodedImage.getEncodedImageResource("message.png");
EncodedImage e5 = EncodedImage.getEncodedImageResource("settings.png");
EncodedImage e6 = EncodedImage.getEncodedImageResource("logout.png");


HorizontalFieldManager buttonPanel = new HorizontalFieldManager(Field.FIELD_TOP | Field.USE_ALL_WIDTH);

  VerticalFieldManager vfm=new VerticalFieldManager();
  vfm.add(new LabelField("Map"));
  vfm.add(new BitmapField(e2.getBitmap(),FOCUSABLE){
     protected boolean navigationClick(int status, int time){

        Dialog.alert("MAP");
        return true;
       }

  });
  buttonPanel.add(vfm);
  buttonPanel.add(new LabelField("  "));

  VerticalFieldManager vfm1=new VerticalFieldManager();
  vfm1.add(new LabelField("Members"));
  vfm1.add(new BitmapField(e3.getBitmap(),FOCUSABLE){
     protected boolean navigationClick(int status, int time){

        Dialog.alert("Members");
        return true;
       }

  });
  buttonPanel.add(vfm1);

  VerticalFieldManager vfm2=new VerticalFieldManager();
  vfm2.add(new LabelField("Message"));
  vfm2.add(new BitmapField(e4.getBitmap(),FOCUSABLE){
     protected boolean navigationClick(int status, int time){

        Dialog.alert("Message");
        return true;
       }

  });
  buttonPanel.add(vfm2);

  VerticalFieldManager vfm3=new VerticalFieldManager();
  vfm3.add(new LabelField("Settings"));
  vfm3.add(new BitmapField(e5.getBitmap(),FOCUSABLE){
     protected boolean navigationClick(int status, int time){

        Dialog.alert("Settings");
        return true;
       }

  });
  buttonPanel.add(vfm3);

  VerticalFieldManager vfm4=new VerticalFieldManager();
  vfm4.add(new LabelField("Logout"));
  vfm4.add(new BitmapField(e6.getBitmap(),FOCUSABLE){
     protected boolean navigationClick(int status, int time){

        Dialog.alert("Logout");
        return true;
       }

  });
  buttonPanel.add(vfm4);

  setStatus(buttonPanel);