Java 我的片段中的按钮无法识别

Java 我的片段中的按钮无法识别,java,android,button,fragment,Java,Android,Button,Fragment,我想测试这个按钮在我的应用程序上是否有效。 它们被放在我的第一块碎片里。当我点击“全部”按钮时,它根本没有响应。我的主要目标是加载fragment,但在本例中,我放置System.out.println只是为了测试我的按钮是否正常工作,但它显然不正常工作 我试着做一个新的项目,它可以工作,但只在主要活动中,我想在我的内部练习选项卡片段 我根本看不到任何错误日志 public class Exercises_Tab extends Fragment { @Null

我想测试这个按钮在我的应用程序上是否有效。 它们被放在我的第一块碎片里。当我点击“全部”按钮时,它根本没有响应。我的主要目标是加载fragment,但在本例中,我放置System.out.println只是为了测试我的按钮是否正常工作,但它显然不正常工作

我试着做一个新的项目,它可以工作,但只在主要活动中,我想在我的内部练习选项卡片段

我根本看不到任何错误日志

    public class Exercises_Tab extends Fragment {

            @Nullable
            @Override
            public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                View Exercises = inflater.inflate(R.layout.fragment_1,container,false);

                Button allbtn = (Button) Exercises.findViewById(R.id.all_button);

            allbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("Test");
                }
            });
                    return Exercises;
                }
            }
和我的XML:

    <LinearLayout
        android:id="@+id/Linear_Layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text= " Muscles "
                android:background="#000000"
                android:textColor="#ffffff"
                android:textSize="25dp"

                android:layout_marginTop="3dp"/>






            <HorizontalScrollView
                android:id="@+id/scrollViewhorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:paddingVertical="-20dp">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">




                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"
                        android:background="#fafafa"
                        android:text="All"
                        android:textColor="#696969"
                        android:id="@+id/all_button"/>

                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:id="@+id/second_button"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"
                        android:background="#fafafa"
                        android:text="Button 1"
                        android:textColor="#696969" />
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_width="80dp"
                        android:layout_height="40dp"
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                </LinearLayout>

            </HorizontalScrollView>

    </LinearLayout>




    <FrameLayout
        android:id="@+id/Frame_Layout"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:layout_centerHorizontal="true"
        android:background="@color/colorPrimary"
        android:layout_below="@+id/Linear_Layout"


        >
    </FrameLayout>


为片段执行此函数中的全部代码,因为它已经引用了您的xml视图

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   Button allbtn = (Button) view.findViewById(R.id.all_button);
   allbtn.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           System.out.println("Test");
           }
        });
 }

简单地说,您可以像下面这样实现click侦听器

public class Exercises_Tab extends Fragment implements OnClickListener {
    Button allbtn ;

    @Override
    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
        View Exercises = inflater.inflate(R.layout.fragment_1,container,false);
        allbtn = (Button) Exercises.findViewById(R.id.all_button);
        allbtn .setOnClickListener(this);
        return Exercises ;
    }

    @Override
    public void onClick(View v) {
        // implements your things
    }
}

我更喜欢这个,因为它使您的代码更具可读性。

@peco您的答案与他们已有的答案相同。最终,在膨胀的
视图上调用
findViewById()
的方法并不重要。
按钮是在充气过程中创建的,而不是在
onCreateView()返回后创建的。@Mike.M所以在onViewCreate或onViewCreated中设置onClickListener并不重要,因为布局是使用充气器.inflate创建的,在本例中,它位于onCreateView方法right的第一行?@peco right。
视图
对象实际上是在调用
充气()
时创建的,因此您可以在调用之后的任何时间修改它们。这些完全相同的对象稍后将被传递到
onViewCreated()
,但自
onCreateView()
以来,这些对象将不会发生任何更改,因此,如果确实需要,您可以在
onCreateView()
中执行所有设置。基本上,onViewCreated()的
onViewCreated()
方法可以更容易地保持代码整洁。(顺便说一句,你没有完全了解我的用户名,所以我没有得到通知。我没有忽视你,或者其他任何事情。)@MikeM。我在我的新活动和ot中尝试过类似的操作,但按钮、视图和充气器在MainActivity中声明,但在实际项目中,它们在Fragment类中,而不是在MainActivity中,由于按钮不工作,我可以做些什么来修复我的问题。顺便说一句,谢谢你的编辑。很难说。
片段
代码很好,看起来这是正确的布局,否则它可能会崩溃。当您单击
按钮时,是否会收到任何视觉反馈?也就是说,它看起来像是被点击了吗?还有,你确定你没有错过日志打印吗?你有没有试过更引人注目的东西,比如
烤面包片
?那么,您是如何加载该
片段的呢?它是否位于
活动
布局中的
元素中?或者你正在用代码进行交易?你确定你在屏幕上看到的真的是练习选项卡吗?