In my code I need to implement a setOnClickListener
but inside a Fragment
but when I try to do it I get an error The setOnClickListener symbol cannot be resolved , could someone tell me how to correct this?
Here is my code:
public class TerminalFragment extends Fragment implements ServiceConnection, SerialListener {
.
.
mHexBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
.
.
}
});
The correct way to assign the listener according to your code is, within the method
onCreateView()
you must inflate the view that contains the button, assign the listener and later, very importantly, return the view that you initially inflated.The main problem showing the error :
It is because the listener is not being initialized within
onCreate()
or in this caseonCreateView()
since you are using aFragment