1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

First coding help


  1. carbonassassin

    carbonassassin New Member

    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 (Text):
    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();
        }
    }
    }

    Advertisement
    :
  2. zarej

    zarej Member

    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()
Loading...

Share This Page