androidandroid-layoutbuttonbuttonclick

Android - Could not find method onButtonClick(View)


I have a problem with the button id "btn_scansiona" once I click on it the following traceback is trigged:

java.lang.IllegalStateException: Could not find method onButtonClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btn_scansiona'
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:325)
                                                                           at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                           at android.view.View.performClick(View.java:4780)
                                                                           at android.view.View$PerformClick.run(View.java:19866)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

fragment_scan.xml

<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.myapp.ScanFragment">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="keywords"
    android:id="@+id/textView3"
    android:layout_gravity="center_horizontal|top"
    android:textStyle="bold"
    android:layout_marginTop="40dp"
    android:textSize="20dp" />

<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="scansiona"
    android:id="@+id/btn_scansiona"
    android:layout_gravity="center_horizontal|bottom"
    android:textStyle="bold"
    android:layout_marginBottom="40dp"
    android:background="#ffaa00"
    android:onClick="onButtonClick" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Insert keywords"
    android:id="@+id/textSearch"
    android:layout_marginTop="80dp"
    android:layout_gravity="center_horizontal|top"
    android:enabled="true"
    android:inputType="text"
    android:textColor="@color/abc_input_method_navigation_guard"
    android:textIsSelectable="false"
    android:textSize="20sp"
    android:textStyle="bold"
    android:selectAllOnFocus="false" />

ScanFragment.java

public class ScanFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public ScanFragment() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment ScanFragment.
 */
// TODO: Rename and change types and number of parameters
public static ScanFragment newInstance(String param1, String param2) {
    ScanFragment fragment = new ScanFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_scan, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mListener = (OnFragmentInteractionListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

//DB
private DbManager db=null;
private CursorAdapter adapter;
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

public void onButtonClick(View view){
    //do something when button is clicked.
    EditText txt=(EditText) view.findViewById(R.id.textSearch);
    if (txt.length()>0 && date.length()>0)
    {
        db.save(txt.getEditableText().toString(), date);
        adapter.changeCursor(db.query());
    }
}

}

If I declare the method "onButtonClick" on MainActivity.java the following traceback disappear but I receive another traceback regarding the "txt.length" cannot be null. Where is my mistake? thanks Daniele


Solution

  • First of all, the onClick method should be present in the parent activity, so that's why the error ain't coming in case you move the code to the activity class

    OR

    If you want to have it in your fragment then you'll have to initialize the button in fragment and set the onClickListener for it, something like this :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
       View view = inflater.inflate(R.layout. fragment_scan,
        container, false);
       Button button = (Button) view.findViewById(R.id.btn_scansiona);
       button.setOnClickListener(new OnClickListener()
       {
                 @Override
                 public void onClick(View v)
                 {
                    // do something
                 } 
       }); 
       return view;
    }
    

    and you're getting txt.length() can't be null error because :

    then you can do your normal processing like this :

    txt.getText().toString().length()