【问题标题】:Blackberry - accessing other class instance contextBlackberry - 访问其他类实例上下文
【发布时间】:2015-06-14 17:54:39
【问题描述】:

我来自 Android,现在我正在为 Blackberry 学习。

在 Android 中访问其他类,我们可以在 java 中传递类似于“this”的上下文。如何在黑莓中做到这一点?问题出在黑莓中,我想在其他类中添加 Screen 类的字段/管理器,代码示例:

public final class MyScreen extends MainScreen
{
//Creates a new MyScreen object

   public MyScreen()
   {        
    // Set the displayed title of the screen       
    setTitle("MyTitle");
    process1 x = new process1(); // will add the labelfield
   }
}

this 在其他文件类中

public class process1
{
   public process1()
   {        
    //i'm trying to get the context of MyScreen so i can add the field in this class
     MyScreen.add(new Labelfield("test"));
    //but its giving error with the message cannot make static reference
   }
}

【问题讨论】:

    标签: class blackberry


    【解决方案1】:

    更改 process1 的构造函数以获取 MyScreen 对象:

    public process1(MyScreen screen)
       {        
         screen.add(new Labelfield("test"));
       }
    

    【讨论】:

    • 并在调用构造函数时使用thisprocess1 x = new process1(this);
    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2017-11-24
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多