Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment Button onClick not firing... what am I missing?
    primarykey
    data
    text
    <p>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. </p> <p>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</p> <pre><code>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; } } } </code></pre> <p>Here's the XML for the fragment:</p> <pre><code>&lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>!!!!!!!!! EDIT !!!!!!!!!</p> <p>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.</p> <p>Thanks to everybody for all the help!!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload