Reputation: 1801
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" />
Upvotes: 107
Views: 338655
Reputation: 1
Even if it's too late, but I have a more convenient solution. First, create a method in the MainActivity
class called insertIntoDb(view: View)
. Second, make sure you have a reference to the fragment in the MainActivity
class. If you're using the Navigation component, you can use the NavHostFragment
to retrieve the currently displayed fragment. After that, check if the current fragment is of the desired fragment type. If it is, cast it to the appropriate fragment class. Then, call the desired method on the fragment instance.
fun insertIntoDb(view: View) {
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_main) as NavHostFragment
val currentFragment = navHostFragment.childFragmentManager.primaryNavigationFragment
if (currentFragment is YourFragment) {
val yourFragment = currentFragment as YourFragment
// Now you have access to the public methods defined in YourFragment class
yourFragment.insertIntoDb(view)
}
}
Upvotes: 0
Reputation: 4342
If you want to use data binding you can follow this solution The following solution might be a better one to follow. the layout is in fragment_my.xml
<data>
<variable
name="listener"
type="my_package.MyListener" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/moreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> listener.onClick()}"
android:text="@string/login"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
And the Fragment would be as follows
class MyFragment : Fragment(), MyListener {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return FragmentMyBinding.inflate(
inflater,
container,
false
).apply {
lifecycleOwner = viewLifecycleOwner
listener = this@MyFragment
}.root
}
override fun onClick() {
TODO("Not yet implemented")
}
}
interface MyListener{
fun onClick()
}
Upvotes: 0
Reputation: 546
For Kotlin users:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?) : View?
{
// Inflate the layout for this fragment
var myView = inflater.inflate(R.layout.fragment_home, container, false)
var btn_test = myView.btn_test as Button
btn_test.setOnClickListener {
textView.text = "hunny home fragment"
}
return myView
}
Upvotes: 6
Reputation: 913
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
Upvotes: 19
Reputation: 133560
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;
}
Upvotes: 216
Reputation: 1447
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.writeqrcode_main, container, false);
// Inflate the layout for this fragment
txt_name = (TextView) view.findViewById(R.id.name);
txt_usranme = (TextView) view.findViewById(R.id.surname);
txt_number = (TextView) view.findViewById(R.id.number);
txt_province = (TextView) view.findViewById(R.id.province);
txt_write = (EditText) view.findViewById(R.id.editText_write);
txt_show1 = (Button) view.findViewById(R.id.buttonShow1);
txt_show1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Onclick","Onclick");
txt_show1.setVisibility(View.INVISIBLE);
txt_name.setVisibility(View.VISIBLE);
txt_usranme.setVisibility(View.VISIBLE);
txt_number.setVisibility(View.VISIBLE);
txt_province.setVisibility(View.VISIBLE);
}
});
return view;
}
You OK !!!!
Upvotes: 3
Reputation: 16832
The others have already said that methods in onClick are searched in activities, not fragments. Nevertheless, if you really want it, it is possible.
Basically, each view has a tag (probably null). We set the root view's tag to the fragment that inflated that view. Then, it is easy to search the view parents and retrieve the fragment containing the clicked button. Now, we find out the method name and use reflection to call the same method from the retrieved fragment. Easy!
in a class that extends Fragment
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_id, container, false);
OnClickFragments.registerTagFragment(rootView, this); // <========== !!!!!
return rootView;
}
public void onButtonSomething(View v) {
Log.d("~~~","~~~ MyFragment.onButtonSomething");
// whatever
}
all activities are derived from the same ButtonHandlingActivity:
public class PageListActivity extends ButtonHandlingActivity
ButtonHandlingActivity.java:
public class ButtonHandlingActivity extends Activity {
public void onButtonSomething(View v) {
OnClickFragments.invokeFragmentButtonHandlerNoExc(v);
//or, if you want to handle exceptions:
// try {
// OnClickFragments.invokeFragmentButtonHandler(v);
// } catch ...
}
}
It has to define methods for all xml onClick handlers.
com/example/customandroid/OnClickFragments.java:
package com.example.customandroid;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Fragment;
import android.view.View;
public abstract class OnClickFragments {
public static class FragmentHolder {
Fragment fragment;
public FragmentHolder(Fragment fragment) {
this.fragment = fragment;
}
}
public static Fragment getTagFragment(View view) {
for (View v = view; v != null; v = (v.getParent() instanceof View) ? (View)v.getParent() : null) {
Object tag = v.getTag();
if (tag != null && tag instanceof FragmentHolder) {
return ((FragmentHolder)tag).fragment;
}
}
return null;
}
public static String getCallingMethodName(int callsAbove) {
Exception e = new Exception();
e.fillInStackTrace();
String methodName = e.getStackTrace()[callsAbove+1].getMethodName();
return methodName;
}
public static void invokeFragmentButtonHandler(View v, int callsAbove) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
String methodName = getCallingMethodName(callsAbove+1);
Fragment f = OnClickFragments.getTagFragment(v);
Method m = f.getClass().getMethod(methodName, new Class[] { View.class });
m.invoke(f, v);
}
public static void invokeFragmentButtonHandler(View v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
invokeFragmentButtonHandler(v,1);
}
public static void invokeFragmentButtonHandlerNoExc(View v) {
try {
invokeFragmentButtonHandler(v,1);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void registerTagFragment(View rootView, Fragment fragment) {
rootView.setTag(new FragmentHolder(fragment));
}
}
And the next adventure will be proguard obfuscation...
PS
It is of course up to you to design your application so that the data live in the Model rather than in Activities or Fragments (which are Controllers from the MVC, Model-View-Controller point of view). The View is what you define via xml, plus the custom view classes (if you define them, most people just reuse what already is). A rule of thumb: if some data definitely must survive the screen turn, they belong to Model.
Upvotes: 4
Reputation: 1833
This is not an issue, this is a design of Android. See here:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
A possible workaround would be to do something like this in your MainActivity:
Fragment someFragment;
...onCreate etc instantiating your fragments
public void myClickMethod(View v){
someFragment.myClickMethod(v);
}
and then in your Fragment class:
public void myClickMethod(View v){
switch(v.getid()){
// Your code here
}
}
Upvotes: 10