为什么按钮不会转到下一个 activity

Why are the buttons not going to the next activity

出于某种原因,我的按钮没有任何作用。我以前用过这种方法来实现按钮,但它从来没有给我带来任何问题。该应用程序有七个不同的按钮,它们都移动到不同的 activity.

public class ScheduleActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mondayButton,tuesdayButton,wednesdayButton,thursdayButton,fridayButton,saturdayButton,sundayButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule);
        mondayButton = findViewById(R.id.monday_button);
        tuesdayButton = findViewById(R.id.tuesday_button);
        wednesdayButton = findViewById(R.id.wednesday_button);
        thursdayButton = findViewById(R.id.thursday_button);
        fridayButton = findViewById(R.id.friday_button);
        saturdayButton = findViewById(R.id.saturday_button);
        sundayButton = findViewById(R.id.sunday_button);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.monday_button:
                Intent monday_intent = new Intent(ScheduleActivity.this, MondayActivity.class);
                startActivity(monday_intent);
                break;
            case R.id.tuesday_button:
                Intent tuesday_intent = new Intent(ScheduleActivity.this, TuesdayActivity.class);
                startActivity(tuesday_intent);
                break;
            case R.id.wednesday_button:
                Intent wednesday_intent = new Intent(ScheduleActivity.this, WednesdayActivity.class);
                startActivity(wednesday_intent);
                break;
            case R.id.thursday_button:
                Intent thursday_intent = new Intent(ScheduleActivity.this, ThursdayActivity.class);
                startActivity(thursday_intent);
                break;
            case R.id.friday_button:
                Intent friday_intent = new Intent(ScheduleActivity.this, FridayActivity.class);
                startActivity(friday_intent);
                break;
            case R.id.saturday_button:
                Intent saturday_intent = new Intent(ScheduleActivity.this, SaturdayActivity.class);
                startActivity(saturday_intent);
            case R.id.sunday_button:
                Intent sunday_intent = new Intent(ScheduleActivity.this, SundayActivity.class);
                startActivity(sunday_intent);
        }
    }
}

您正在获取按钮的实例,但从未为它们设置 OnClickListener。您需要为按钮设置点击监听器:

mondayButton.setOnClickListener(this)

您需要对所有按钮执行此操作,这会告诉您的代码在单击按钮时通知事件的位置。

您没有将侦听器 View.OnClickListener 附加到您的任何按钮。 在你初始化你的按钮后在你的 onCreate() 中添加这个,你的按钮将工作

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_schedule);
        ...
        sundayButton = findViewById(R.id.sunday_button);

        // attaching listeners
        mondayButton.setOnClickListener(this);
        tuesdayButton.setOnClickListener(this);
        wednesdayButton.setOnClickListener(this);
        thursdayButton.setOnClickListener(this);
        fridayButton.setOnClickListener(this);
        saturdayButton.setOnClickListener(this);
        sundayButton.setOnClickListener(this);
   }

您必须在 OnCreate 方法上设置 view.setOnClickListener{}

私有按钮 mondayButton,tuesdayButton,wednesdayButton,thursdayButton,fridayButton,saturdayButton,sundayButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule);
    mondayButton = findViewById(R.id.monday_button);
    tuesdayButton = findViewById(R.id.tuesday_button);
    wednesdayButton = findViewById(R.id.wednesday_button);
    thursdayButton = findViewById(R.id.thursday_button);
    fridayButton = findViewById(R.id.friday_button);
    saturdayButton = findViewById(R.id.saturday_button);
    sundayButton = findViewById(R.id.sunday_button);


    mondayButton.setOnClickListener() {
    Intent intent = new Intent(...) ;
    startActivity(intent) ;
   } 
}