1

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103

2 Answers2

1

If for example you have a layout with multiple Buttons like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/btnOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

You can assign a different id to each Button, in this example the first Button has the id R.id.btnOne and the second Button has R.id.btnTwo.

You can reference these Buttons in code by calling findViewById() with the id of the View you are looking for. So to get the first Button you have to do this:

Button btnOne = (Button) findViewById(R.id.btnOne);

And the second Button which has the id R.id.btnTwo can be retrieved like this:

Button btnTwo = (Button) findViewById(R.id.btnTwo);

One you have a reference to each Button you can assign a different click listener to each one of them:

Button btnOne = (Button) findViewById(R.id.btnOne);        
btnOne.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        foo();
    }
});


Button btnTwo = (Button) findViewById(R.id.btnTwo);
btnTwo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        bar();
    }
});
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
  • This is my mainactivity.java: http://paste.ofcode.org/K7Nq64XSuLRGaF7F3TNGPi, this is MyPagerAdapter: http://paste.ofcode.org/FKMrNtULdUj3ypG4QFt6aD: main.xml: http://paste.ofcode.org/WJg7i2gpB3QhmW4nRhzkBu, this is when.xml: http://paste.ofcode.org/3aJQS2AUiMFwDEhfkfUTQ9M and this is what.xml: http://paste.ofcode.org/W2grnCgfn2DVRTsvNUfymB – Ivanka Todorova Jul 10 '14 at 11:42
  • Why do you post all these links? I'm not going to look through all of them. Edit the relevant parts into your question. – Xaver Kapeller Jul 10 '14 at 11:44
  • Because I'm aware of these things you have written and thanks about explaining them. But the buttons are in different files and when I put this code in `onCreate` the application crashes (I posted the erorr messages). My question is how to handle the buttons over the files. – Ivanka Todorova Jul 10 '14 at 11:48
  • You cannot combine multiple files, one `Activity` or one `Fragment` can only have one layout and you can only access `Views` from that particular layout. – Xaver Kapeller Jul 10 '14 at 11:53
  • You are getting a `NullPointerException` because you are looking for a `Button` with the id `R.id.when_button`, but in the layout of your `Activity` there is no such `View`. – Xaver Kapeller Jul 10 '14 at 11:55
  • As I see this method `instantiateItem` returns the view of the layout that is being included, can I use this and set some events for the buttons inside this view? – Ivanka Todorova Jul 10 '14 at 12:00
  • Which method do you mean? See, that would be something you should include in your question, how is anybody supposed to know what you are talking about. Anyway, no and you should not do that. Each `Activity` or `Fragment` should only deal with its own layout. – Xaver Kapeller Jul 10 '14 at 12:03
1

If you want buttons on your fragments, you have to define the buttons in your fragment's layouts.

Then, inside your fragment you can define the button this way:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_fragment, container, false);

    Button button1 = (Button) view.findViewById(R.id.button1);
    //...

    button1.setOnClickListener(this);

    return view;
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.button1:
            //DO WORK
            break;
    }
}

Also you should extend OnClickListener in your fragment:

public class MyFragment extends Fragment implements OnClickListener {
masmic
  • 3,526
  • 11
  • 52
  • 105