每当我运行我的应用程序时,它都工作正常,但一旦我单击分配的 Textview 以意图执行另一个 Activity,我的应用程序就会崩溃。
Button backlogin;
TextView elecName, appointmentName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
// Initialize UI components
backlogin = findViewById(R.id.backlogin);
elecName = findViewById(R.id.elecName);
appointmentName = findViewById(R.id.appointmentName);
backlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an intent to navigate back to the Login activity
Intent intent = new Intent(AdminActivity.this, Login.class);
startActivity(intent);
// Finish the AdminActivity
finish();
elecName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an intent to navigate to RecycleViewActivity
Intent intent = new Intent(AdminActivity.this, RecycleViewActivity.class);
startActivity(intent);
appointmentName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an intent to navigate to AppointmentFragment
Intent intent = new Intent(AdminActivity.this, AppointmentFragment.class);
startActivity(intent);
我真的不知道问题是什么,请帮忙。
我尝试放置 clickTextView 方法,但出现错误。
验证您是否已正确初始化 TextView 并检查目标活动中的 onCreate 方法是否存在任何潜在问题。虽然识别问题可能具有挑战性,但共享您的崩溃日志将极大地促进故障排除过程,从而更轻松地查明和解决问题。
如果 AppointmentFragment 是一个 Fragment,则无法使用类似 Activity 的 Intent 来启动 Fragment。你可以试试下面的代码吗?
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new AppointmentFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();