• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps First coding help

My first coding project is a soundboard type thing on andriod using eclipse (on 2.3.3) and im really confused as to whats wrong with my code, I dont get any errors but the button doesnt play the sounds. If anyone could have a quick look at the code that would be great
Code:
package com.mkyong.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class AppActivity extends Activity {

	Button button;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		addListenerOnButton();
	}

	public void addListenerOnButton() {

		final Context context = this;

		button = (Button) findViewById(R.id.button1);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Intent intent = new Intent(context, App2Activity.class);
                startActivity(intent);   

			}

		});

	}

	public class PlayIt extends Activity implements OnClickListener {
		
    MediaPlayer mp1;
    MediaPlayer mp2;

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mp1 = MediaPlayer.create(this, R.raw.froze);
        mp2 = MediaPlayer.create(this, R.raw.lasagna);

        final Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener((OnClickListener) this);

        final Button button = (Button) findViewById(R.id.button02);
        button.setOnClickListener((OnClickListener) this);

    }

    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.button1:
            mp1.start();
            break;
        case R.id.button02:
            mp2.start();
            break;
        }
    }

    @Override
    protected void onDestroy() {
        mp1.release();
        mp2.release();
        super.onDestroy();
    }
}
}
 
In first class AppActivity you set on button click listener to go to App2Activity, row:
Intent intent = new Intent(context, App2Activity.class);

Your another class is activity named PlayIt. You should change upper row to:
Intent intent = new Intent(context, PlayIt.class);

Another thing, Activity PlayIt should report error because you need to put setContenView(R.layout.your_layout) before using findViewById()
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones