for current class:

public class MainActivity extends AppCompatActivity implements PermissionManager.PermissionListener {

    private MaterialButton startStopButton;
    private LinearLayout activityListLayout;

    private boolean isServiceRunning = false;

    private BroadcastReceiver transitionBroadcastReceiver;

    private DatabaseHelper databaseHelper;

    private static final int MAX_ACTIVITIES = 4; // Maximum number of activities to display

    private PermissionManager permissionManager;
    private PreferenceManager preferenceManager;

    private boolean userRequestedStart = false; // Flag to track if user requested to start the service

    // UI for sensors
    // Sensors Card UI elements
    private MaterialButton sensorStartStopButton;
    private MaterialButton expandCollapseButton;
    private LinearLayout sensorListLayout;

    private boolean isSensorCardExpanded = false;

    private SensorRecorder sensorRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize database helper
        DatabaseHelper.initializeInstance(this);
        databaseHelper = DatabaseHelper.getInstance();

        startStopButton = findViewById(R.id.startStopButton);
        activityListLayout = findViewById(R.id.activityListLayout);

        // Initialize PreferenceManager
        preferenceManager = PreferenceManager.getInstance(this);
        isServiceRunning = preferenceManager.isServiceRunning();

        // Update UI based on service state
        updateStartStopButton();

        // Initialize PermissionManager
        permissionManager = PermissionManager.getInstance(this);
        permissionManager.setPermissionListener(this);

        sensorStartStopButton.setOnClickListener(v -> {
            if (sensorRecorder.isRecording()) {
                stopSensorRecording();
            } else {
                startSensorRecording();
            }
        });

        startStopButton.setOnClickListener(v -> {
            if (isServiceRunning) {
                stopActivityRecognitionService();
            } else {
                // Before starting, ensure permissions are granted
                userRequestedStart = true;
                permissionManager.checkAndRequestPermissions(this);
            }
        });


        // Load previous activities from database (optional)
        loadPreviousActivities();

        // If service is running, start the service and register receiver
        if (isServiceRunning) {
            startActivityRecognitionService();
        }
        // Initialize SensorRecorder
        sensorRecorder = new SensorRecorder(this, new SensorRecorder.SensorDataListener() {
            @Override
            public void onSensorData(SensorEvent event) {
                // Handle sensor data here
                // For example, log it or process it as needed
                Log.d("MainActivity", "Sensor: " + event.sensor.getName() + ", Values: " + Arrays.toString(event.values));
            }
        });
    }

    private void startSensorRecording() {
        Intent serviceIntent = new Intent(this, SensorService.class);
        ContextCompat.startForegroundService(this, serviceIntent);

        sensorStartStopButton.setText(getString(R.string.stop));
        sensorStartStopButton.setIcon(ContextCompat.getDrawable(this, R.drawable.stop));

        Toast.makeText(this, "Started recording sensor data", Toast.LENGTH_SHORT).show();
    }

    private void stopSensorRecording() {
        Intent serviceIntent = new Intent(this, SensorService.class);
        stopService(serviceIntent);

        sensorStartStopButton.setText(getString(R.string.start));
        sensorStartStopButton.setIcon(ContextCompat.getDrawable(this, R.drawable.play));

        Toast.makeText(this, "Stopped recording sensor data", Toast.LENGTH_SHORT).show();
    }
    private void toggleSensorCardExpansion() {
        if (isSensorCardExpanded) {
            // Collapse the card
            sensorListLayout.setVisibility(View.GONE);
            expandCollapseButton.setIcon(ContextCompat.getDrawable(this, R.drawable.fullscreen_exit));
            isSensorCardExpanded = false;
        } else {
            // Expand the card and display the sensors list
            displaySensorsList();
            sensorListLayout.setVisibility(View.VISIBLE);
            expandCollapseButton.setIcon(ContextCompat.getDrawable(this, R.drawable.fullscreen));
            isSensorCardExpanded = true;
        }
    }

    private void displaySensorsList() {
        // Clear the list first to avoid duplicates
        sensorListLayout.removeAllViews();

        List<Sensor> deviceSensors = sensorRecorder.getDeviceSensors();

        for (Sensor sensor : deviceSensors) {
            String sensorInfo = sensor.getName() + " (" + sensor.getType() + ")\nVendor: " + sensor.getVendor();

            TextView sensorTextView = new TextView(this);
            sensorTextView.setText(sensorInfo);
            sensorTextView.setPadding(8, 8, 8, 8);

            sensorListLayout.addView(sensorTextView);
        }
    }

    private void updateStartStopButton() {
        if (isServiceRunning) {
            startStopButton.setText(R.string.stop);
            startStopButton.setIcon(ContextCompat.getDrawable(this, R.drawable.stop));
        } else {
            startStopButton.setText(R.string.start);
            startStopButton.setIcon(ContextCompat.getDrawable(this, R.drawable.play));
        }
    }

    private void startActivityRecognitionService() {
        Intent serviceIntent = new Intent(this, ActivityTransitionService.class);
        ContextCompat.startForegroundService(this, serviceIntent);

        // Register receiver to update UI
        if (transitionBroadcastReceiver == null) { // prevent multiple registrations
            transitionBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String activityInfo = intent.getStringExtra("activity");
                    updateActivityList(activityInfo);
                }
            };
            LocalBroadcastManager.getInstance(this).registerReceiver(transitionBroadcastReceiver, new IntentFilter("TRANSITION_ACTION"));
        }

        Toast.makeText(this, "Started activity recognition service", Toast.LENGTH_SHORT).show();
        isServiceRunning = true;
        preferenceManager.setServiceRunning(true);
        updateStartStopButton();

        requestIgnoreBatteryOptimizations();
    }

    private void stopActivityRecognitionService() {
        Intent serviceIntent = new Intent(this, ActivityTransitionService.class);
        stopService(serviceIntent);

        if (transitionBroadcastReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(transitionBroadcastReceiver);
            transitionBroadcastReceiver = null;
        }

        Toast.makeText(this, "Stopped activity recognition service", Toast.LENGTH_SHORT).show();
        isServiceRunning = false;
        preferenceManager.setServiceRunning(false);
        updateStartStopButton();
    }

    private void updateActivityList(String activityInfo) {
        // Add the new activity to the database
        databaseHelper.insertTransitionEvent(System.currentTimeMillis(), activityInfo, "Transition");

        // Reload the activity list
        loadPreviousActivities();
    }

    private void loadPreviousActivities() {
        // Clear current views
        activityListLayout.removeAllViews();

        // Get the last 4 activities from the database
        List<String> recentActivities = databaseHelper.getLastNActivities(MAX_ACTIVITIES);

        // If there are no activities, display a message
        if (recentActivities.isEmpty()) {
            TextView noDataTextView = new TextView(this);
            noDataTextView.setText(R.string.no_activities_recorded_yet);
            noDataTextView.setPadding(8, 8, 8, 8);
            activityListLayout.addView(noDataTextView);
            return;
        }

        // Display the activities
        for (int i = 0; i < recentActivities.size(); i++) {
            String activity = recentActivities.get(i);

            TextView activityTextView = new TextView(this);
            activityTextView.setText(activity);
            activityTextView.setPadding(8, 8, 8, 8);

            // Highlight the last activity (current activity)
            if (i == recentActivities.size() - 1) {
                activityTextView.setTypeface(null, Typeface.BOLD);
            }

            activityListLayout.addView(activityTextView);
        }
    }

    private void requestIgnoreBatteryOptimizations() {
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        String packageName = getPackageName();
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(android.net.Uri.parse("package:" + packageName));
            startActivity(intent);
        }
    }

    // Forward the permission result to PermissionManager
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        permissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    // PermissionManager.PermissionListener implementation
    @Override
    public void onPermissionsGranted() {
        Toast.makeText(this, "All permissions granted", Toast.LENGTH_SHORT).show();
        if (userRequestedStart) {
            startActivityRecognitionService();
            userRequestedStart = false; // Reset the flag
        }
        // Perform any other actions if needed
    }

    @Override
    public void onPermissionsDenied(List<String> deniedPermissions) {
        Toast.makeText(this, "Permissions denied: " + deniedPermissions.toString(), Toast.LENGTH_LONG).show();
        if (userRequestedStart) {
            userRequestedStart = false; // Reset the flag
        }
    }

    @Override
    protected void onDestroy() {
        // Don't stop the service here to allow it to continue running in the background
        super.onDestroy();
    }
}

I`ve got errors:
2024-09-23 03:42:27.623 23515-23515 AndroidRuntime          com.google.sl                        E  FATAL EXCEPTION: main (Ask Gemini)
                                                                                                    Process: com.google.sl, PID: 23515
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.sl/com.google.sl.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.button.MaterialButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                                    	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3946)
                                                                                                    	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4126)
                                                                                                    	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:108)
                                                                                                    	at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:195)
                                                                                                    	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:157)
                                                                                                    	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:90)
                                                                                                    	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2544)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:232)
                                                                                                    	at android.os.Looper.loop(Looper.java:317)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8501)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
                                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.button.MaterialButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                                    	at com.google.sl.ui.MainActivity.onCreate(MainActivity.java:87)
                                                                                                    	at android.app.Activity.performCreate(Activity.java:8767)
                                                                                                    	at android.app.Activity.performCreate(Activity.java:8745)
                                                                                                    	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1519)
                                                                                                    	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3928)
                                                                                                    	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4126) 
                                                                                                    	at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:108) 
                                                                                                    	at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:195) 
                                                                                                    	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:157) 
                                                                                                    	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:90) 
                                                                                                    	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2544) 
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:232) 
                                                                                                    	at android.os.Looper.loop(Looper.java:317) 
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8501) 
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) 
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878) 
2024-09-23 03:42:27.628 23515-23515 Process                 com.google.sl                        I  Sending signal. PID: 23515 SIG: 9
---------------------------- PROCESS ENDED (23515) for package com.google.sl ----------------------------