Java Android:在不同的类中声明UI元素
我有一个扩展Activity的主类,它超过2k行,所以我尝试在另一个类中编写代码,但不确定它应该扩展什么。我已经声明了一个上下文,并从我的主类将其作为上下文发送,但是当我尝试类似于Java Android:在不同的类中声明UI元素,java,android,class,user-interface,extends,Java,Android,Class,User Interface,Extends,我有一个扩展Activity的主类,它超过2k行,所以我尝试在另一个类中编写代码,但不确定它应该扩展什么。我已经声明了一个上下文,并从我的主类将其作为上下文发送,但是当我尝试类似于Context.findViewById()的操作时,它不会允许我这样做 这里的最佳实践是什么 public class HomeScreen extends Activity implements OnItemSelectedListener{ //somewhere in a method, in
Context.findViewById()
的操作时,它不会允许我这样做
这里的最佳实践是什么
public class HomeScreen extends Activity implements OnItemSelectedListener{
//somewhere in a method, in an onClickListener
new Profile(getApplicationContext()).userCreate();
}
public class Profile{
Context ctx;
int userID;
public Profile (Context ctx)
{
this.ctx=ctx;
}
public void userCreate(){
Button create = (Button) ctx.findViewById()// can't call find view
}
}
如果你不发布问题类的代码,我们就无法知道你在说什么。首先,不要在问题标题前加上像Android这样的标记词,底部的标记就足够了。其次,要么将活动引用直接传递到
概要文件
(因此概要文件构造函数将是公共的概要文件(活动上下文)
),要么将上下文参数强制转换到活动(但需要传递活动引用而不是应用程序上下文),然后使用findViewById()
method.如何获取活动引用?如何获取活动引用?-在主屏幕
活动新建配置文件(此)中创建配置文件实例时,只需通过此
。
。注意:这必须从非静态方法开始,从onCreate()
开始;我通过了我的课程。这是因为我在一所大学里