Search code examples
androidmedia-playersoundpool

Soundboard App force closes and I can't figure out why


So I've been working on a soundboard app for my kids. This is my first app so as you can imagine I very nearly have no idea what I'm doing (noob) so I'm apologizing in advance :-). I'm not sure where my problem is but my splash screen runs no problem but when it tries to load the next activity it force closes. i'm going to include my manifest the java file that is supposed to play the audio and the layout for the buttons which are clickable images. Thanks in advance! Also I would like to set it up where the buttons could play a random sound that relates to the image using soundpool but again with the noobness. I'm not really familiar at all with the errors but I'm seeing java.land.classcastexception: android.widget.imageview as the reason the mymenu activity isn't starting. Hope that helps.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pnl.thebasics"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/sssicon"
    android:label="@string/app_name" >
    <activity android:label="@string/app_name" android:name=".myMain">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/app_name" android:name=".myMenu">
        <intent-filter>
            <action android:name="com.pnl.thebasics.CLEARSCREEN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>



</application>

</manifest>


package com.pnl.thebasics;

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

public class myMenu extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    // Hide the title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Go full screen
    final Window window = getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.menu);

    //these are the buttons that play sounds

    //button 1 (sponge bob)
    final MediaPlayer mpButtonClick1 = MediaPlayer.create(this, R.raw.sb1);
    Button bSpongebob = (Button) findViewById(R.id.sbbutton);
    bSpongebob.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            mpButtonClick1.start();
        }
    });
    //button 2 (patrick)
    final MediaPlayer mpButtonClick2 = MediaPlayer.create(this, R.raw.pat1);
    Button bPatrick = (Button) findViewById(R.id.patbutton);
    bPatrick.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            mpButtonClick2.start();
        }
    });
}



}


<?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"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/LinearLayout01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/sbbutton"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_weight="50"
        android:clickable="true"
        android:src="@drawable/sbbuttonimage" />

    <ImageView
        android:id="@+id/patbutton"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_weight="50"
        android:clickable="true"
        android:src="@drawable/patbuttonimage" />
</LinearLayout>



<LinearLayout
    android:id="@+id/LinearLayout02"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/mrcrabsbutton"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_weight="50"
        android:clickable="true"
        android:src="@drawable/mrcrabsbuttonimage" />

    <ImageView
        android:id="@+id/squidwardbutton"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_weight="50"
        android:clickable="true"
        android:src="@drawable/squidwardbuttonimage" />
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout03"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/planktonbutton"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_weight="50"
        android:clickable="true"
        android:src="@drawable/planktonbuttonimage" />

    <ImageView
        android:id="@+id/garybutton"
        android:layout_width="wrap_content"
        android:layout_height="130dp"
        android:layout_weight="50"
        android:clickable="true"
        android:src="@drawable/garybuttonimage" />
</LinearLayout>

</LinearLayout>

Solution

  • Your Java code is expecting to find a Button object in your layout:

    Button bSpongebob = (Button) findViewById(R.id.sbbutton);
    

    But your layout declares that widget to be an ImageView:

    <ImageView
        android:id="@+id/sbbutton"
    

    An ImageView isn't a Button, and when your Java code tries to force it to be a Button you get the java.lang.ClassCastException.

    Two choices to fix:

    1) Change your Java code to use ImageView.

    2) Change your layout to declare a Button.

    Either will accept the click listener you're trying to set. Don't forget you need to make this fix for both widgets in your app.