I'm currently using ViewPager
to slide through different Layouts
.
I have two so far and these two Layouts
have Buttons
.
I have a main Layout and two layouts which are loaded in the main layout.
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:text="@string/when"
android:id="@+id/when_button"
style="@style/ShadowText"
android:background="@drawable/button_background_states"
android:gravity="center"
android:padding="10dp"
android:adjustViewBounds="true"
android:textColor="@color/text"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
This is one of my buttons. But I want every button on every layout to be clicked. And when the buttons clicked to be played a sound:
final Button button = (Button) findViewById(R.id.when_button); button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),
R.raw.when);
mp.start();
} });
This used to be in my onCreate
, but now it makes my app crash. I'm a ultra newbie, I just cannot think of a way to set different events on those different buttons.
Process: com.phakeapps.mladmerindzhey.app, PID: 18970
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phakeapps.mladmerindzhey.app/com.phakeapps.mladmerindzhey.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.phakeapps.mladmerindzhey.app.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I'm following this tutorial
I do believe the problem is that the buttons are in different files, but I'm not sure how to find them when a layout is changed.