Почему мое приложение не запускает новое действие с использованием звуковых пулов?
Привет, я новичок в Android, и я добавил новое действие в моем приложении, похожее на деки, каждая из 8 клавиш воспроизводит новый звук, и есть 2 кнопки, которые меняют клавиши на новые звуки, а изображение на каждой клавише на новые изображения, но он закрывается каждый раз, когда я открываю его, пробовал несколько исправлений, но ничего даже близко не закрылось, и все предложения приветствуются, вот мой код
activity_music.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MusicActivity"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/secondLayout"
android:layout_marginRight="11dp">
<ImageButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/imageButton"
android:src="@drawable/lionkey"
android:scaleType="fitCenter"
android:padding="5dp"
android:background="@drawable/patched"
android:layout_weight="1"
android:contentDescription="@string/lionhead"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/imageButton2"
android:src="@drawable/dogkey"
android:scaleType="fitCenter"
android:background="@drawable/patched2"
android:padding="5dp"
android:layout_weight="1"
android:contentDescription="@string/doghead"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/imageButton3"
android:src="@drawable/catkey"
android:scaleType="fitCenter"
android:background="@drawable/patched3"
android:padding="5dp"
android:layout_weight="1"
android:contentDescription="@string/cathead"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/imageButton4"
android:src="@drawable/duckkey"
android:scaleType="fitCenter"
android:padding="5dp"
android:background="@drawable/patched"
android:layout_weight="1"
android:contentDescription="@string/duckhead"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/imageButton5"
android:src="@drawable/monkeykey"
android:scaleType="fitCenter"
android:padding="5dp"
android:background="@drawable/patched2"
android:layout_weight="1"
android:contentDescription="@string/monkeyhead"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/imageButton6"
android:background="@drawable/patched3"
android:src="@drawable/sheepkey"
android:scaleType="fitCenter"
android:padding="5dp"
android:layout_weight="1"
android:contentDescription="@string/sheephead"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/imageButton7"
android:src="@drawable/pigkey"
android:scaleType="fitCenter"
android:padding="5dp"
android:background="@drawable/patched"
android:layout_weight="1"
android:contentDescription="@string/pighead"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="60dp"
android:id="@+id/imageButton8"
android:src="@drawable/elephantkey"
android:scaleType="fitCenter"
android:padding="5dp"
android:background="@drawable/patched2"
android:layout_weight="1"
android:contentDescription="@string/elephanthead"/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/secondLayout"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button2"
android:layout_below="@+id/Button1"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button3"
android:layout_below="@+id/Button2"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button4"
android:layout_below="@+id/Button3"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button5"
android:layout_below="@+id/Button4"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button6"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</RelativeLayout>
и моя ява
Музыкальная активность java
package com.martinsapp.socialstories;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MusicActivity extends Activity {
SoundPool sp;
int instrument=1;
int lionSound=0;
int dogsound=0;
int catsound =0;
int ducksound =0;
int monkeysound =0;
int sheepsound=0;
int pigsound=0;
int elephantsound=0;
int lettera=0;
int letterc=0;
int letterd=0;
int letterg=0;
int letterk=0;
int lettero=0;
int lettert=0;
int letterw=0;
ImageButton btn1;
ImageButton btn2;
ImageButton btn3;
ImageButton btn4;
ImageButton btn5;
ImageButton btn6;
ImageButton btn7;
ImageButton btn8;
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
sp = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
dogsound = sp.load(this, R.raw.puppy_dog,1);
catsound = sp.load(this, R.raw.kittenmeow,1);
sheepsound = sp.load(this, R.raw.sheep,1);
ducksound = sp.load(this, R.raw.quack_quack,1);
pigsound = sp.load(this, R.raw.pig_snort,1);
monkeysound = sp.load(this, R.raw.monkey_noise,1);
lionSound = sp.load(this, R.raw.lion_roar,1);
elephantsound = sp.load(this, R.raw.lion_roar,1);
lettera = sp.load(this, R.raw.letter_a,1);
letterc = sp.load(this, R.raw.letter_c,1);
letterd = sp.load(this, R.raw.letter_d,1);
letterg = sp.load(this, R.raw.letter_g,1);
letterk = sp.load(this, R.raw.letter_k,1);
lettero = sp.load(this, R.raw.letter_o,1);
lettert = sp.load(this, R.raw.letter_t,1);
letterw = sp.load(this, R.raw.letter_w,1);
btn1 = (ImageButton)findViewById(R.id.imageButton);
btn1.setOnClickListener(ButtonClickListener);
btn2 = (ImageButton)findViewById(R.id.imageButton2);
btn2.setOnClickListener(ButtonClickListener);
btn3 = (ImageButton)findViewById(R.id.imageButton3);
btn3.setOnClickListener(ButtonClickListener);
btn4 = (ImageButton)findViewById(R.id.imageButton4);
btn4.setOnClickListener(ButtonClickListener);
btn5 = (ImageButton)findViewById(R.id.imageButton5);
btn5.setOnClickListener(ButtonClickListener);
btn6 = (ImageButton)findViewById(R.id.imageButton6);
btn6.setOnClickListener(ButtonClickListener);
btn7 = (ImageButton)findViewById(R.id.imageButton7);
btn7.setOnClickListener(ButtonClickListener);
btn8 = (ImageButton)findViewById(R.id.imageButton8);
btn8.setOnClickListener(ButtonClickListener);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(ButtonClickListener);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(ButtonClickListener);
if (instrument == 1){
btn1.setImageResource(R.drawable.lionkey);
btn2.setImageResource(R.drawable.dogkey);
btn3.setImageResource(R.drawable.catkey);
btn4.setImageResource(R.drawable.duckkey);
btn5.setImageResource(R.drawable.monkeykey);
btn6.setImageResource(R.drawable.sheepkey);
btn7.setImageResource(R.drawable.pigkey);
btn8.setImageResource(R.drawable.elephantkey);
}
}
private OnClickListener ButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.imageButton:
if (instrument == 1){
if (lionSound != 0){
sp.play(lionSound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (lettera != 0){
sp.play(lettera, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton2:
if (instrument == 1){
if (dogsound != 0){
sp.play(dogsound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (letterc != 0){
sp.play(letterc, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton3:
if (instrument == 1){
if (catsound != 0){
sp.play(catsound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (letterd != 0){
sp.play(letterd, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton4:
if (instrument == 1){
if (ducksound != 0){
sp.play(ducksound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (letterg != 0){
sp.play(letterg, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton5:
if (instrument == 1){
if (monkeysound != 0){
sp.play(monkeysound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (letterk != 0){
sp.play(letterk, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton6:
if (instrument == 1){
if (sheepsound != 0){
sp.play(sheepsound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (lettero != 0){
sp.play(lettero, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton7:
if (instrument == 1){
if (pigsound != 0){
sp.play(pigsound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (lettert != 0){
sp.play(lettert, 1, 1, 0, 0, 1);}}
break;
case R.id.imageButton8:
if (instrument == 1){
if (elephantsound != 0){
sp.play(elephantsound, 1, 1, 0, 0, 1);}}
if (instrument == 2){
if (letterw != 0){
sp.play(letterw, 1, 1, 0, 0, 1);}}
break;
case R.id.Button1:
instrument = 1;
if (instrument == 1){
btn1.setImageResource(R.drawable.lionkey);
btn2.setImageResource(R.drawable.dogkey);
btn3.setImageResource(R.drawable.catkey);
btn4.setImageResource(R.drawable.duckkey);
btn5.setImageResource(R.drawable.monkeykey);
btn6.setImageResource(R.drawable.sheepkey);
btn7.setImageResource(R.drawable.pigkey);
btn8.setImageResource(R.drawable.elephantkey);
}
break;
case R.id.Button2:
instrument = 2;
if (instrument == 2){
btn1.setImageResource(R.drawable.cat_a);
btn2.setImageResource(R.drawable.cat_c);
btn3.setImageResource(R.drawable.d);
btn4.setImageResource(R.drawable.g);
btn5.setImageResource(R.drawable.k);
btn6.setImageResource(R.drawable.o);
btn7.setImageResource(R.drawable.cat_t);
btn8.setImageResource(R.drawable.w);
}
}
}
};
}
а вот logcat для когда приложение закрывается
02-01 11:11:42.300 22216-22216/com.martinsapp.socialstories E/AndroidRuntime﹕ FATAL
EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.martinsapp.socialstories/
com.martinsapp.socialstories.
MusicActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.martinsapp.socialstories.MusicActivity.onCreate(MusicActivity.java:86)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:22
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)
1 ответ
Решение
Рад, что никто не ответил на это, так как это была моя собственная кнопка глупости = findviewbyid R.Id.button1 должен быть R.Id.Button1 Теперь он работает