Java 如何使用按钮调用类

Java 如何使用按钮调用类,java,android,class,Java,Android,Class,我想使用片段中创建的按钮调用一个类。这是包含按钮的片段的代码 public class FragmentSecondPage extends Fragment{ View root; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){ root = inflater.inflate(R.layout.fragme

我想使用片段中创建的按钮调用一个类。这是包含按钮的片段的代码

public class FragmentSecondPage extends Fragment{

    View root;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){
        root = inflater.inflate(R.layout.fragment_page2, container, false); 
        return root;
    }
}

我想从按钮present infragment_page2启动一个class SplashActivity.class。有人能帮我吗?

首先,你可以开始使用谷歌:如何在点击按钮时开始新的活动。然后单击第一个结果,进入stackoverflow。这与我回答的您有什么不同?问题是我在行按钮bt=Button findViewByIdR.id.button1处收到错误消息;我必须将其转换为Button bt=Button root.findviewbydr.id.button1;请告诉我两者的区别,以便将来参考,我让它自己运行代码。谢谢。请对代码进行注释…片段是活动的一部分,因此它不是活动findviewbyid是类活动的方法而不是片段,因此您需要使用getActivity.whateverfunction调用当前活动
//Create Intent that would call the Next Activity and since you are in Fragment you need to call getActivity() instead of this
Intent myIntent = new Intent(getActivity(), SplashActivity.class);
// This function will replace the currently running activity with new one
getActivity().startActivity(myIntent);