Help debugging

Discussion in 'Application Development' started by maest, Aug 5, 2010.

  1. maest

    maest Member
    16

    Aug 5, 2010
    23
    2
    16
    Hi,
    I'm trying to write a simple app that plays a sound when a button is clicked and then stops playing it when the button is clicked again. I'll just copy-paste the code:
    package tutorial.helloviews;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class HelloViews extends Activity {
    boolean playing = false;
    MediaPlayer mp = MediaPlayer.create(this, R.raw.play);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.play);
    if(mp != null)
    {
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {
    if(playing)
    {
    mp.start();
    playing = true;
    }
    else
    {
    mp.stop();
    playing = false;
    }
    }
    });}
    }
    }

    There is a file called play.mp3 in /res/raw. When I try and run it I receive an error saying the app stopped unexpectedly. I've attached a screenshot from the debug window.

    Is it something wrong with the code? Or is it the emulator's fault?

    Does the play.mp3 file get uploaded on the emulator as well as part of the app?
     

    Attached Files:


    Advertisement

  2. markb

    markb Well-Known Member
    63

    Sep 14, 2009
    148
    26
    63
    Software Developer (Java, C, Web, Unix)
    Birmingham, UK
    Hi,

    Please use the CODE tags when posting code so that indentation & layout are preserved. It makes it easier for people to read your code, and so they'll be more likely to help.

    Also, you might want to consider pasting the text of the exception and traceback from the logs rather than a screenshot. (Also in CODE tags.)

    Without looking at the code, I can be pretty sure it's not the emulator's fault.

    Have you configured your activity in the Android Manifest XML file?
    (If you're not sure, past your file into a reply, using CODE tags.)

    Mark
     
  3. maest

    maest Member
    16

    Aug 5, 2010
    23
    2
    16
    Sorry about the CODE tag business. Was in a rush when I first posted this and didn't notice the tag.
    Code (Text):
    1. package tutorial.helloviews;
    2.  
    3. import android.app.Activity;
    4. import android.media.MediaPlayer;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.Button;
    8.  
    9. public class HelloViews extends Activity {
    10.     boolean playing = false;
    11.     MediaPlayer mp = MediaPlayer.create(this, R.raw.play);
    12.     /** Called when the activity is first created. */
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.main);
    17.        
    18.         Button button = (Button) findViewById(R.id.play);
    19.         if(mp != null)
    20.         {
    21.         button.setOnClickListener(new View.OnClickListener() {
    22.             public void onClick(View v)
    23.             {
    24.                 if(playing)
    25.                 {
    26.                     mp.start();
    27.                     playing = true;
    28.                 }
    29.                 else
    30.                 {
    31.                     mp.stop();
    32.                     playing = false;
    33.                 }
    34.             }
    35.         });}
    36.     }
    37. }
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:layout_width="fill_parent"
    4.     android:layout_height="fill_parent">
    5.     <Button android:id="@+id/play"
    6.     android:text="Play"
    7.     android:layout_width="wrap_content"
    8.     android:layout_height="wrap_content">
    9.     </Button>
    10. </LinearLayout>
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.       package="tutorial.helloviews"
    4.       android:versionCode="1"
    5.       android:versionName="1.0">
    6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    7.         <activity android:name=".HelloViews"
    8.                   android:label="@string/app_name">
    9.             <intent-filter>
    10.                 <action android:name="android.intent.action.MAIN" />
    11.                 <category android:name="android.intent.category.LAUNCHER" />
    12.             </intent-filter>
    13.         </activity>
    14.  
    15.     </application>
    16.  
    17.  
    18. </manifest>
    and

    Code (Text):
    1. Thread [<1> main] (Suspended (exception RuntimeException))    
    2.     ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585    
    3.     ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679    
    4.     ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125    
    5.     ActivityThread$H.handleMessage(Message) line: 2033    
    6.     ActivityThread$H(Handler).dispatchMessage(Message) line: 99    
    7.     Looper.loop() line: 123    
    8.     ActivityThread.main(String[]) line: 4627    
    9.     Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]    
    10.     Method.invoke(Object, Object...) line: 521    
    11.     ZygoteInit$MethodAndArgsCaller.run() line: 868    
    12.     ZygoteInit.main(String[]) line: 626    
    13.     NativeStart.main(String[]) line: not available [native method]    
    14.  
     
  4. andywhoa

    andywhoa Well-Known Member
    38

    May 15, 2010
    105
    13
    38
    Software Engineer
    West Des Moines, IA
    For starters:
    Code (Text):
    1.                 if(playing)
    should be
    Code (Text):
    1.                 if(!playing)
    Also, set mp in the onCreate method:
    Code (Text):
    1. mp = MediaPlayer.create(this, R.raw.play);
    I've never worked with a MediaPlayer before; so I can't say your parameters are correct. They don't make sense to me.
     
    maest likes this.
  5. maest

    maest Member
    16

    Aug 5, 2010
    23
    2
    16
    Cool, that worked, thanks. I just had to move the creation of mp to inside onCreate(). That was an oversight on my behalf.
     

Share This Page

Loading...