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

Apps Help debugging

maest

Newbie
Aug 5, 2010
23
2
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?
 

Attachments

  • debug.JPG
    debug.JPG
    102 KB · Views: 89
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.)

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

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
 
Upvote 0
Sorry about the CODE tag business. Was in a rush when I first posted this and didn't notice the tag.
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;
                }
            }
        });}
    }
}

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:id="@+id/play"
    android:text="Play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </Button>
</LinearLayout>

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="tutorial.helloviews"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloViews"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>


</manifest>

and

Code:
Thread [<1> main] (Suspended (exception RuntimeException))    
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585    
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679    
    ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125    
    ActivityThread$H.handleMessage(Message) line: 2033    
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99    
    Looper.loop() line: 123    
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]    
    Method.invoke(Object, Object...) line: 521    
    ZygoteInit$MethodAndArgsCaller.run() line: 868    
    ZygoteInit.main(String[]) line: 626    
    NativeStart.main(String[]) line: not available [native method]
 
Upvote 0
For starters:
Code:
                if(playing)
should be
Code:
                if(!playing)

Also, set mp in the onCreate method:
Code:
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.
 
  • Like
Reactions: maest
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