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;
    }
}
有帮助吗?

解决方案 2

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" />

其他提示

If you want to do custom coding, you need to learn langauges like Objective-C or Swift to develop iOS app. Or another way is mobile app development platform. With the help of app development tools you can develop apps with drag & Drop facilities without writing a single line of code.

I am an iOS app developer, I have tried most of the mobile app development platforms. I have developed more than 50 apps till today with the help of Phonegap, Telerik, Configure.IT etc. They are running successfully on app store.

As per my experience in this field, I recommend developers as well as beginners to use mobile app development platform like http://www.configure.it/, because it provides automatic coding, app preview facility, direct API connect and a lot more features. These things save a lot more development time and provides fast and well designed app in much less time.

The main benefit of this tool, it is web based platform so you do not require to buy Mac system, you can make an app from any place as well as from any system.

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
    Button button6 = (Button)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(getApplicationContext(), com.example.lullabymain.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;
}

}

When you set the context of {this} you are using the {this} of the onClickListener. use getApplicationContext();

Button button6 = (Button)findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        startActivity(new Intent(getApplicationContext(), Alphabet.class));
    }
});

What are the errors that your code is throwing?

What you need to do is set on click listeners on the required buttons and followed by Intents to go to your new class for example Intent i5 = new Intent(this, HadithList.class); startActivity(i5);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top