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):
    1. package com.mkyong.android;
    2.  
    3. import android.app.Activity;
    4. import android.content.Context;
    5. import android.content.Intent;
    6. import android.media.MediaPlayer;
    7. import android.os.Bundle;
    8. import android.widget.Button;
    9. import android.view.View;
    10. import android.view.View.OnClickListener;
    11.  
    12. public class AppActivity extends Activity {
    13.  
    14.     Button button;
    15.  
    16.     @Override
    17.     public void onCreate(Bundle savedInstanceState) {
    18.         super.onCreate(savedInstanceState);
    19.         setContentView(R.layout.main);
    20.         addListenerOnButton();
    21.     }
    22.  
    23.     public void addListenerOnButton() {
    24.  
    25.         final Context context = this;
    26.  
    27.         button = (Button) findViewById(R.id.button1);
    28.  
    29.         button.setOnClickListener(new OnClickListener() {
    30.  
    31.             @Override
    32.             public void onClick(View arg0) {
    33.  
    34.                 Intent intent = new Intent(context, App2Activity.class);
    35.                 startActivity(intent);  
    36.  
    37.             }
    38.  
    39.         });
    40.  
    41.     }
    42.  
    43.     public class PlayIt extends Activity implements OnClickListener {
    44.        
    45.     MediaPlayer mp1;
    46.     MediaPlayer mp2;
    47.  
    48.     public void onCreate1(Bundle savedInstanceState) {
    49.         super.onCreate(savedInstanceState);
    50.  
    51.         mp1 = MediaPlayer.create(this, R.raw.froze);
    52.         mp2 = MediaPlayer.create(this, R.raw.lasagna);
    53.  
    54.         final Button button1 = (Button) findViewById(R.id.button1);
    55.         button1.setOnClickListener((OnClickListener) this);
    56.  
    57.         final Button button = (Button) findViewById(R.id.button02);
    58.         button.setOnClickListener((OnClickListener) this);
    59.  
    60.     }
    61.  
    62.     public void onClick(View v) {
    63.         switch(v.getId()) {
    64.         case R.id.button1:
    65.             mp1.start();
    66.             break;
    67.         case R.id.button02:
    68.             mp2.start();
    69.             break;
    70.         }
    71.     }
    72.  
    73.     @Override
    74.     protected void onDestroy() {
    75.         mp1.release();
    76.         mp2.release();
    77.         super.onDestroy();
    78.     }
    79. }
    80. }

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

Share This Page