I'm trying to invoke the method in my onClick (View v) XML, but does not work with Fragment. This is the error.
01-17 12:38:36.840: E/AndroidRuntime(4171): java.lang.IllegalStateException: Could not find a method insertIntoDb(View) in the activity class main.MainActivity for onClick handler on view class android.widget.Button with id 'btn_conferma'
Java code:
public void insertIntoDb(View v) { ... }
XML:
<Button android:id="@id/btn_conferma" style="?android:attr/borderlessButtonStyle" android:layout_width="0.0dip" android:layout_height="45dp" android:layout_marginLeft="2dp" android:layout_weight="1.0" android:background="@drawable/bottoni" android:gravity="center_horizontal|center_vertical" android:onClick="insertIntoDb" android:text="SALVA" android:textColor="#ffffff" android:textSize="16sp" />
Using the android:onClick XML attribute where you just use the name of a public method with the signature void name(View v) or by using the setOnClickListener method where you pass an object that implement the OnClickListener interface.
Your activity must have
public void insertIntoDb(View v) { ... }
not Fragment .
If you don't want the above in activity. initialize button in fragment and set listener to the same.
<Button android:id="@+id/btn_conferma" // + missing
Then
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_rssitem_detail, container, false); Button button = (Button) view.findViewById(R.id.btn_conferma); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do something } }); return view; }
Another option may be to have your fragment implement View.OnClickListener and override onClick(View v) within your fragment. If you need to have your fragment talk to the activity simply add an interface with desired method(s) and have the activity implement the interface and override its method(s).
public class FragName extends Fragment implements View.OnClickListener { public FragmentCommunicator fComm; public ImageButton res1, res2; int c; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_test, container, false); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { fComm = (FragmentCommunicator) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement FragmentCommunicator"); } } @Override public void onActivityCreated(Bundle savedInstanceState) { res1 = (ImageButton) getActivity().findViewById(R.id.responseButton1); res1.setOnClickListener(this); res2 = (ImageButton) getActivity().findViewById(R.id.responseButton2); res2.setOnClickListener(this); } public void onClick(final View v) { //check for what button is pressed switch (v.getId()) { case R.id.responseButton1: c *= fComm.fragmentContactActivity(2); break; case R.id.responseButton2: c *= fComm.fragmentContactActivity(4); break; default: c *= fComm.fragmentContactActivity(100); break; } public interface FragmentCommunicator{ public int fragmentContactActivity(int b); } public class MainActivity extends FragmentActivity implements FragName.FragmentCommunicator{ int a = 10; //variable a is update by fragment. ex. use to change textview or whatever else you'd like. public int fragmentContactActivity(int b) { //update info on activity here a += b; return a; } }
http://developer.android.com/training/basics/firstapp/starting-activity.html http://developer.android.com/training/basics/fragments/communicating.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With