我试图以编程方式生成一组按钮,并使它们可以在片段中单击。但是,我得到了:
error: cannot find symbol method OnClickListener(TagsFragment)
到目前为止我的代码:
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");
}
}
此外,android studio突出显示"标签"在这一行:
tag.OnClickListener(this);
我有这个:" Expected class or package
"
答案 0 :(得分:2)
替换此行:
tag.OnClickListener(this);
with:
tag.setOnClickListener(this);