I'm making it so a button makes sound when clicked and I keep running into an error. heres my java code:
package com.thisisfoo.testgame;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
MediaPlayer buttonSound;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Button sounds
final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.test);
//setting up buttons
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
buttonSound.start();
startActivity(new Intent("com.thisisfoo.testgame"));
}
});
}
It plays the sound when I run it, but it crashes and says "unfortunately, TEST has stopped". Can anyone help me please. Thanks!
edit: heres my logcat
02-02 22:56:29.401: E/AndroidRuntime(27328): FATAL EXCEPTION: main
02-02 22:56:29.401: E/AndroidRuntime(27328): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.thisisfoo.testgame }
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1638)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1510)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.app.Activity.startActivityForResult(Activity.java:3263)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.app.Activity.startActivity(Activity.java:3370)
02-02 22:56:29.401: E/AndroidRuntime(27328): at com.thisisfoo.testgame.Main$1.onClick(Main.java:35)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.view.View.performClick(View.java:3538)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.view.View$PerformClick.run(View.java:14319)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.os.Handler.handleCallback(Handler.java:608)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.os.Looper.loop(Looper.java:156)
02-02 22:56:29.401: E/AndroidRuntime(27328): at android.app.ActivityThread.main(ActivityThread.java:5060)
02-02 22:56:29.401: E/AndroidRuntime(27328): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 22:56:29.401: E/AndroidRuntime(27328): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 22:56:29.401: E/AndroidRuntime(27328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-02 22:56:29.401: E/AndroidRuntime(27328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-02 22:56:29.401: E/AndroidRuntime(27328): at dalvik.system.NativeStart.main(Native Method)
as in log:
ActivityNotFoundException: No Activity found to handle Intent
because you are not passing right action to start next activity. try to start next Activity as :
Intent intent=new Intent(Main.this,Your_Next_Activity.class);
startActivity(intent);
This crash is because of your media player is playing sound in main activity context while you start another activity. you can solve this by different way eg either you start this media playerin thread or wait for your media player to stop and then start another activity
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.