gpt4 book ai didi

java - 找不到符号方法 OnClickListener android

转载 作者:行者123 更新时间:2023-12-02 06:00:27 24 4
gpt4 key购买 nike

我正在尝试以编程方式生成一组按钮并使它们在 fragment 中可单击。但是,我得到:

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);

我有这个:“预期的类或包

最佳答案

替换这一行:

 tag.OnClickListener(this);

与:

tag.setOnClickListener(this);

关于java - 找不到符号方法 OnClickListener android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com