我试图以编程方式生成一组按钮,并使它们在片段中可单击。但是,我得到:
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”
Expected class or package
替换此行:
与:
tag.setOnClickListener(this);