Eclipse Programmers ....I am trying to design the app that has 3 buttons on the main screen . So that when the user presses on 1st two buttons...it plays the different music located in the raw folder. The 3rd button on the main screen should drive the user to the next screen which also has buttons.I tried to run my app on the emulator..It plays the music on first two buttons of the main screen , but when I click on the third (Next) button . It says "Unfortunately Your App has stopped". I Don't know what's wrong with my code...So any help will be greatly appreciated... My MAIN and Second class JAVA code, main.xml and activity_second.xml... looks like the following
Main Java Code
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class Audio extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
}
public void onClick(View v) {
int resId = 1;
switch (v.getId()) {
case R.id.button_1: resId = R.raw.button_1; break;
case R.id.button_2: resId = R.raw.button_2; break;
case R.id.button_3:
startActivity(new Intent(Audio.this,SecondActivity.class));
break;
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
Second Class Java Code
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
public class SecondActivity extends Activity {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
public void onClick(View v) {
int resId = 1;
switch (v.getId()) {
case R.id.button_4: resId = R.raw.button_4; break;
case R.id.button_5: resId = R.raw.button_5; break;
}
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
Main.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/directions" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:stretchColumns="*" >
<Button
android:id="@+id/button_1"
android:text="@string/_1" />
<Button
android:id="@+id/button_2"
android:text="@string/_2" />
<Button
android:id="@+id/button_3"
android:text="@string/_3" />
</TableLayout>
</LinearLayout>
activity_second.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/directions" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:stretchColumns="*" >
<Button
android:id="@+id/button_4"
android:text="@string/_4" />
<Button
android:id="@+id/button_5"
android:text="@string/_5" />
</TableLayout>
</LinearLayout>
No comments:
Post a Comment