Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:16:20+00:00 2026-06-15T13:16:20+00:00

I have created two classes. I want to have a button on class 1

  • 0

I have created two classes. I want to have a button on class 1 (MainActivity) that when it is pressed, it will take me to class 2 (Alphabet). I have tried numerous ways of doing it and I have been unsuccessful. Here is my original code below. Can anyone help me?
Sorry, I am new to app developing.

package com.example.lullabymain;


import android.os.Bundle;  
import android.app.Activity;
import android.view.Menu;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.View;
import android.view.View.OnClickListener;




public class MainActivity extends Activity implements OnClickListener {

private MediaPlayer mp;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // new code

        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
    }


    public void onClick(View v) {
        int resId = 0;
        switch (v.getId()) {
        case R.id.button1: resId = R.raw.rockabye; break;
        case R.id.button2: resId = R.raw.hushlittlebaby; break;
        case R.id.button3: resId = R.raw.twinkle; break;
        case R.id.button4: resId = R.raw.hickory; break;
        case R.id.button5: resId = R.raw.oldmcd; break;


        }
        //release any resources from previous mediaplayer
        if (mp != null) {
            mp.release();
        }
        //create a new mediaplayer to play this sound
        mp = MediaPlayer.create(this, resId);
        mp.start();

    }


    @Override
    protected void  onStop()
    {
        //stop audio
        super.onStop();
        mp.stop();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

The code below is the code that I attempted which includes ‘Intent’

package com.example.lullabymain;

import android.os.Bundle; 

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.media.AudioManager;

import android.media.MediaPlayer;

import android.view.View;

import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {
    private MediaPlayer mp;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // new code
        **View button6 = findViewById(R.id.button6);
        button6.setOnClickListener(this);**       
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
    }


    public void onClick(View v) {
        int resId = 0;
        switch (v.getId()) {
        case R.id.button1: resId = R.raw.rockabye; break;
        case R.id.button2: resId = R.raw.hushlittlebaby; break;
        case R.id.button3: resId = R.raw.twinkle; break;
        case R.id.button4: resId = R.raw.hickory; break;
        case R.id.button5: resId = R.raw.oldmcd; break;
        **case R.id.button6:
            Intent i = new Intent(this, Alphabet.class);
            startActivity(i);
            break;**

        }
        //release any resources from previous mediaplayer
        if (mp != null) {
            mp.release();
        }
        //create a new mediaplayer to play this sound
        mp = MediaPlayer.create(this, resId);
        mp.start();

    }


    @Override
    protected void  onStop()
    {
        //stop audio
        super.onStop();
        mp.stop();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:16:21+00:00Added an answer on June 15, 2026 at 1:16 pm

    Currently in MainActivity Activity you are not adding setOnClickListener to button6 but in onClick method you are trying to start Activity on button6 click . to get your code working add setOnClickListener to button6 also as

    public class MainActivity extends Activity implements OnClickListener {
    private MediaPlayer mp;
    Button button6 ;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // new code
        button6 = (Button)findViewById(R.id.button6);
        button6.setOnClickListener(this);      
    

    and register Alphabet Activity in Manifest as :

    <activity android:name=".Alphabet" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created two viewController classes such that one is superclass of another.i have
I have created two classes: Initiator and Acceptor. I want to send messages from
I have created two classes: public class Params : List<Param> { } public class
I have created two classes that implement AuthorizeAttribute . One is used globally, and
I have an HTML button created and I want to toggle the text that
Say I have two classes created work and workItem. CWorker *work = new CWorker();
I have two classes with many-to-many relation so I created a join table in-between
I have two model classes, say parent and child. Parents are created first, and
I have created two WindowSurface (WPF), and I want to navigate betwen them. I've
I have two classes called Person and Animal that have a lot of methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.