1

When my app launches, the first thing that happens is a fragment comes to the front with a yes/no question. Depending on your answer, it replaces itself with the appropriate fragment. But what's currently killing me is that the 2 button's onclicks aren't even being called. I've simplified the code for here.

What's weird is that when I run the same code in an Activity instead of a fragment, it works fine. Let me know what you think. Thank you

import android.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class FirstLaunchFragment extends Fragment implements OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_first_launch, container,
                false);

        Button mYesButton = (Button) view.findViewById(R.id.b_yes_mom);
        mYesButton.setOnClickListener(this);

        Button mNoButton = (Button) view.findViewById(R.id.b_no_mom);
        mNoButton.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.b_yes_mom:
                Toast.makeText(getActivity(), "YES is clicked!", 
                                           Toast.LENGTH_LONG).show();
                break;

            case R.id.b_no_mom:
                Toast.makeText(getActivity(), "NO is clicked!", 
                                           Toast.LENGTH_LONG).show();
                break;
        }
    }
}

Here's the XML for the fragment:

<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"
android:background="#BCED91"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<ImageView
    android:id="@+id/iv_stork_and_baby"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignTop="@+id/iv_stork_and_baby"
    android:scaleType="fitCenter"
    android:src="@drawable/storkbaby" />

<ImageView
    android:id="@+id/iv_app_name_png"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/iv_stork_and_baby"
    android:layout_centerHorizontal="true"
    android:scaleType="fitCenter"
    android:src="@drawable/cooltext1349499090" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/iv_stork_and_baby"
    android:layout_centerHorizontal="true"
    android:text="@string/are_you_mom" />

<Button
    android:id="@+id/b_no_mom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/iv_stork_and_baby"
    android:text="NO" />

<Button
    android:id="@+id/b_yes_mom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/iv_stork_and_baby"
    android:layout_below="@+id/iv_stork_and_baby"
    android:text="YES" />

</RelativeLayout>

!!!!!!!!! EDIT !!!!!!!!!

Because this has been making my brain itch, I created a new project, and started copying/pasting the code bit by bit to see what was breaking the functionality. At the end... it all worked. It's exactly the same as above but it works in this project but not the previous one. I think android was just being picky.

Thanks to everybody for all the help!!!

Psest328
  • 6,575
  • 11
  • 55
  • 90
  • 1
    b_yes_mom != b_yes_button. 3 seconds of debugging would have told you that. – njzk2 Dec 30 '13 at 20:17
  • That was a bad copy/paste, it's correct in my project. Just fixed OP – Psest328 Dec 30 '13 at 20:18
  • Nothing seems wrong in your code. So try another way: try to make an **onClick** method **from your Buttons in xml** like http://developer.android.com/guide/topics/ui/controls/button.html#HandlingEvents - Don't really know if it will solved this, but you can try. – Blo Dec 30 '13 at 20:29
  • Put a breakpoint in onClick and see if it even gets called. – zmbq Dec 30 '13 at 20:29
  • @zmbq I did previously, and it's not. – Psest328 Dec 30 '13 at 20:35
  • @Fllo giving it a shot now, will post back with the result – Psest328 Dec 30 '13 at 20:36
  • It should be working. Try to implement OnClickListener for each button and see if that works. – zmbq Dec 30 '13 at 20:40
  • @zmbq lol, I tried that too. I've been scratching my head on this one for 2 days. Best I can tell, the code right but something in the way android is handling the fragment is broken. – Psest328 Dec 30 '13 at 20:42
  • @Fllo That worked, if you post it as an answer, I'll accept it. but that still doesn't solve the riddle as to why the code isn't working as-is. Thank you – Psest328 Dec 30 '13 at 20:43
  • You probably restarted the emulator since you started, right? – zmbq Dec 30 '13 at 20:43
  • Did you try the same in a side project? Create an activity with a fragment and a button, and see if you get the onClick event properly? – zmbq Dec 30 '13 at 20:44
  • @zmbq I don't use an emulator, I have a nexus 4 and a Galaxy S3. the nexus is running 4.4.2 and the S3 just updated to S3 and both get the same behavior – Psest328 Dec 30 '13 at 20:44
  • @zmbq I tried it in an test activity to see if it would work, and it does, but I didn't try it in a test fragment in that activity – Psest328 Dec 30 '13 at 20:45
  • Try it in a test fragment in that activity, just to be sure. It should work, which will bring us to the question of how your fragment is created. One crazy hunch - do you perhaps mix the support library fragments with plain fragments? – zmbq Dec 30 '13 at 20:46
  • @zmbq I don't target below 4.0.3 anymore so I don't use the support libraries at all – Psest328 Dec 30 '13 at 20:51
  • And if you set own onClickListener inline? Try it just for test – Hruskozrout Dec 30 '13 at 20:54
  • @Hruskozrout same behavior as the code above – Psest328 Dec 30 '13 at 20:58

3 Answers3

2

You need to use onClick method directly in your xml like this:

<Button
    android:id="@+id/b_no_mom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/iv_stork_and_baby"
    android:text="NO"
    android:onClick="NoClicked" />

<Button
    android:id="@+id/b_yes_mom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/iv_stork_and_baby"
    android:layout_below="@+id/iv_stork_and_baby"
    android:text="YES"
    android:onClick="YesClicked" />

And in your FragmentActivity class:

public void YesClicked(View v) {
    // Do something
}

public void NoClicked(View v) {
    // Do something
}  

Hope this helps.

Blo
  • 11,903
  • 5
  • 45
  • 99
  • the second solution yields the same behavior as implementing onClick. The first does work, though. Thank you for all your help – Psest328 Dec 30 '13 at 20:53
  • Ok, I'll edit and remove that part. But, I do not resolved your issue precisely, I make a compromise that worked. ;) – Blo Dec 30 '13 at 20:58
  • lol, you get the check my friend. When I get some free time, I'm going to do what @zmbq suggested and create a test activity with a fragment and button and see what's going on – Psest328 Dec 30 '13 at 21:00
  • Let us know if this will change, and edit your question if you will have another strange issue with this. Good luck! – Blo Dec 30 '13 at 21:02
  • @Fllo does this work?? have you tried this??. check this http://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and-xml-onclick – Raghunandan Dec 31 '13 at 04:59
  • @WizardKnight http://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and-xml-onclick – Raghunandan Dec 31 '13 at 05:00
  • @Raghunandan Apparently, it works, but it's not related to this question as you mentioned. It's not an app crash... It's just never called with `setOnClickListener` (and this is weird, because, when I make it like WizardKnight did, I had nothing wrong and this method was called). – Blo Dec 31 '13 at 11:34
  • @Fllo why is not related? it is. I can't get the solution posted working. – Raghunandan Dec 31 '13 at 11:36
  • @Raghunandan Oh, I see the mistake! I was thinking about write fragmentactivity instead of fragment, so I did not think that your answer differe than mine - I'll edit this stupid error, sorry about that. Thanks for pointing this! – Blo Dec 31 '13 at 11:58
  • @Fllo my answer to the question was to the one before op edited. I was wondering how this would work. – Raghunandan Dec 31 '13 at 11:59
0

Make sure the ids match the ones in layout xml

  case  R.id.b_yes_mom: 
     // do something
  break; 
  case R.id.b_no_mom:
      //do something
  break;
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I'm so sorry, that's an issue with me inputting into here. Fixing OP now (they're both yes_mom in my project) – Psest328 Dec 30 '13 at 20:17
  • @WizardKnight pls make sure you post the right code. also post the xml layout fro fragment – Raghunandan Dec 30 '13 at 20:18
  • Added xml to OP. When I copied/pasted fragment, I accidentally copied the onclick from a test activity (the one that runs the same code perfectly) – Psest328 Dec 30 '13 at 20:20
  • That's why I find it so frustrating. When I run the code, nothing happens when you click the buttons. I had a log.d in the onclick method to see if it was being called at all but nope – Psest328 Dec 30 '13 at 20:22
  • @WizardKnight sorry i don't think anybody can fix this coz it seems everything is right is the xml posted `fragment_first_launch.xml` – Raghunandan Dec 30 '13 at 20:24
  • @WizardKnight i see nothing wrong. can't help further good luck – Raghunandan Dec 30 '13 at 20:26
-1

The lines like this:

    android:layout_below="@+id/iv_stork_and_baby"

should be like this:

     android:layout_below="@id/iv_stork_and_baby"

I.e. drop the "+" that causes the id to be defined. Can't say why it worked as an activity but not as a fragment - that might be unrelated, or you introduced the problem in the changeover etc.

Peter vdL
  • 4,953
  • 10
  • 40
  • 60