Home > Enterprise >  Button click not switching activity
Button click not switching activity

Time:10-04

I've been trying to get this app to switch activities by simply clicking the button, but the app simply refreshes and I stay on the same activity. Nothing happens. And if I uncomment the lines of code for my second activity, the OnClick ones, I get null pointer errors. I have no idea what I could be doing wrong. Here's my code in main activity:

package com.example.commontaskslite;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.Button;
import android.widget.CompoundButton;
import java.util.Calendar;


public class MainActivity extends AppCompatActivity {
    private TimePicker alarmClock;
    private PendingIntent pendingIntent;
    private AlarmManager alarmManager;
    private Button search;
    private Button redial;
    private Button address;
    boolean checked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        alarmClock = (TimePicker) findViewById(R.id.timePicker);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);

        search = (Button) findViewById(R.id.search);
        redial = (Button) findViewById(R.id.redial);
        address = (Button) findViewById(R.id.address);

        search.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                openSearch(view);
            }
        });

        redial.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                openRedial(view);
            }
        });

        address.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                openShowAddress(view);
            }
        });

        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                if(isChecked){
                    checked = true;
                    onToggleClicked();
                }
                else{
                    checked = false;
                    onToggleClicked();
                }
            }
        });
    }

    public void openSearch(View V){
        Intent _intent = new Intent(MainActivity.this,Search.class);
        startActivity(_intent);
    }

    public void openRedial(View V){
        Intent _intent = new Intent(MainActivity.this,Redial.class);
        startActivity(_intent);
    }

    public void openShowAddress(View V){
        Intent _intent = new Intent(MainActivity.this,ShowAddress.class);
        startActivity(_intent);
    }

    public void onToggleClicked(){
        long time;
        if(checked) {
            Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.HOUR_OF_DAY, alarmClock.getHour());
            calendar.set(Calendar.MINUTE, alarmClock.getMinute());

            Intent intent = new Intent(this, AlarmReceiver.class);

            pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

            time = (calendar.getTimeInMillis() - (calendar.getTimeInMillis() % 60000));

            if (System.currentTimeMillis() > time) {
                if (calendar.AM_PM == 0) {
                    time = time   (1000 * 60 * 60 * 12);
                } else {
                    time = time   (1000 * 60 * 60 * 24);
                }
            }
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
        }
        else{
            alarmManager.cancel(pendingIntent);
            Toast.makeText(MainActivity.this,"ALARM OFF", Toast.LENGTH_SHORT).show();
            AlarmReceiver.ringtone.stop();
        }
    }

}

Here's the code in the second activity:

package com.example.commontaskslite;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;

public class Search extends AppCompatActivity {
    private PendingIntent pendingIntent;
    private Button alarm;
    private Button redial;
    private Button address;
    private SearchView searchView;
    private String searchQuery;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        intent = getIntent();/*
        address = (Button) findViewById(R.id.address2);
        redial = (Button) findViewById(R.id.redial2);
        alarm = (Button) findViewById(R.id.alarm);
        searchView = (SearchView) findViewById(R.id.searchView);

        searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                searchQuery = s;
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });

        address.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                openShowAddress(view);
            }
        });

        redial.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                openRedial(view);
            }
        });

        alarm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                openMainActivity(view);
            }
        });*/

    }

    public void openShowAddress(View V){
        Intent _intent = new Intent(this,ShowAddress.class);
        startActivity(_intent);
    }

    public void openRedial(View V){
        Intent _intent = new Intent(this,Redial.class);
        startActivity(_intent);
    }

    public void openMainActivity(View V){
        Intent _intent = new Intent(this,MainActivity.class);
        startActivity(_intent);
    }

}

And here's the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.commontaskslite">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CommonTasksLite">
        <activity
            android:name=".ShowAddress"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.ALL_APPS"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Redial"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.CALL"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".Search"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReceiver">
        </receiver>
    </application>


</manifest>

CodePudding user response:

summarizing the answer that was found in comments.

Change setContentView(R.layout.activity_main); to another layout other than activity_main in Search Activity.

  • Related