replace this line :
tag.OnClickListener(this);
with :
tag.setOnClickListener(this);
Question
I'm trying to generate a group of buttons programatically and make them clickable in a fragment. However, I get:
error: cannot find symbol method OnClickListener(TagsFragment)
That's my code so far:
public class TagsFragment extends Fragment implements View.OnClickListener {
public TagsFragment() {
}
public static TagsFragment newInstance() {
TagsFragment fragment = new TagsFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tags, container, false);
Bundle bundle = this.getArguments();
String[] tags = bundle.getStringArray("tags");
for(String item : tags) {
//System.out.println(item);
Button tag = new Button(getActivity());
tag.setText(item);
tag.setTag("newtag");
tag.OnClickListener(this);
((LinearLayout) rootView).addView(tag);
}
return rootView;
}
@Override
public void onClick(View view) {
System.out.println("onclick");
}
}
Also, android studio highlights the "tag" on this line:
tag.OnClickListener(this);
and i have this : "Expected class or package
"
Solution
replace this line :
tag.OnClickListener(this);
with :
tag.setOnClickListener(this);