我试图从非活动类(简单类)调用一个活动 .
我创建了一个标签式活动 . 然后我为片段创建了一个布局 . 并创建了一个单独的类,我在其中进行一些验证并调用一个活动 . 但我无法从非活动类开始活动 .
这是我遇到问题的功能
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.guarding_services_layout, container, false);
p_guard_srvc = p_guard_srvc.findViewById(R.id.guard_booknow_btn);
p_guard_srvc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//this line, i am unable to write the correct method
Intent i = new Intent( GuardingScreen.this , HomeScreen.class);
}
});
return rootView;
}
2 回答
您应该将活动上下文传递给非活动类,并使用上下文启动您的活动 . 例如:您可以在Non-Activity类的构造函数中传递活动上下文,也可以为上下文创建setter方法 .
试试这个 :