Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
这里的MainActivity是当前活动的名称,SecondActivity是要启动的活动的名称。
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new AnotherFragment())
.commit();
其中R.id.fragment_container是要替换的Fragment容器的ID。
检查代码中是否有任何语法错误或逻辑错误,尤其是在判断语句(if/else)或循环语句(for/while)中。
最后,如果您仍然遇到问题,请尝试清除应用程序缓存并重新启动应用程序。如果问题仍然存在,请尝试使用Android Studio提供的调试工具找出问题的根本原因。