我已经实现了onClickListener,但在onCreateView()中,它无法找到按钮的id,即“myButton”
我的代码是:
public class Category extends Fragment implements View.OnClickListener {
View view;Button b;
public Category() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_category, container,false);
b=(Button)view.findViewById(R.id.myButton);
b.setOnClickListener(this);
return view;
}
我的fragment_category.xml如下所示,它包含一个按钮和一个textView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.Category"
android:id="@+id/constraintLayout">
<TextView
android:layout_width="368dp"
android:layout_height="495dp"
android:text="@string/hello_blank_fragment"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="Button"
tools:layout_editor_absoluteX="135dp"
tools:layout_editor_absoluteY="208dp" />
答案 0 :(得分:1)
你正在膨胀&#34; fragment_category&#34;,但你说你发布了&#34;片段&#34; xml代码。
您很可能不会夸大正确的布局文件。
答案 1 :(得分:0)
实际上,经过一些整改后,IDE中出现了问题,错误已被整理出来。