Java 无法在片段中显示Toast

Java 无法在片段中显示Toast,java,android,xml,fragment,Java,Android,Xml,Fragment,在这里,我添加了Java和Xml文件,我只想在用户单击按钮时显示一个Toast 这只是为了检查片段是否正常工作 在片段中,onClickListener不适用于我。当我尝试时,即使是TextView上的setText也不起作用。我认为组件有问题,或者java和xml文件的连接有问题 Setting.java package com.example.dell.jsonexp; import android.content.Context; import android.c

在这里,我添加了Java和Xml文件,我只想在用户单击按钮时显示一个Toast

这只是为了检查片段是否正常工作

在片段中,onClickListener不适用于我。当我尝试时,即使是TextView上的setText也不起作用。我认为组件有问题,或者java和xml文件的连接有问题

Setting.java

    package com.example.dell.jsonexp;

    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class setting extends Fragment {
        BackgroundTask b;
    String e;
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View RootView = inflater.inflate(R.layout.setting, container, false);

            final Button logout= (Button) RootView.findViewById(R.id.logout);
            logout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),"Button Pressed",Toast.LENGTH_LONG).show();


                }
            });

            return inflater.inflate(R.layout.setting, container, false);
        }


    }
setting.xml

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">

        <Button
            android:id="@+id/logout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="50dp"
            android:text="Logout" />
    </RelativeLayout>

您正在将单击侦听器设置为从未显示的按钮

您需要返回rootView,而不是膨胀另一个视图

return rootView;

片段中
无法访问
onCreateView()
方法中的视图组件,必须
覆盖
onViewCreated()
方法。 请更新您的代码如下

    package com.example.dell.jsonexp;

    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class setting extends Fragment {
        BackgroundTask b;
        String e;

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            return inflater.inflate(R.layout.setting, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            Button logout= (Button) view.findViewById(R.id.logout);
            logout.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),"Button Pressed",Toast.LENGTH_LONG).show();


                }
            });
        }

    }
谢谢

您需要返回

return RootView;
对于祝酒词你必须写

getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getContext(), "Whatever you want here", Toast.LENGTH_SHORT).show();
        }
    });