按钮在片段中失败

Button fail in fragment

我在片段 class 中定义了一个按钮和一个编辑文本,但是当我通过从主 activity 按下列表视图启动它时,它崩溃了,我尝试将编辑postData 中的文本并且它有效,但我仍然需要让按钮也能正常工作,我无法切换它的位置,因为我需要单击它,无论如何,我尝试删除 findviewbyid 并只是放置 onclicklistiner 但它让我得到相同的 Nullexcpection也,所以我认为它可能是来自 Fragment it self 的东西。

public class CommunityFragment extends Fragment {
final String myTag = "DocsUpload";
EditText pstTxt;
Button pstBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //pstBtn = (Button) getView().findViewById(R.id.PostButton);

    View rootView = inflater.inflate(R.layout.fragment_community,
            container, false);

    /*pstBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {*/
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    postData();

                }

                private void postData() {
                    try {
                        pstTxt = (EditText) getView().findViewById(R.id.PostText);
                        String fullUrl = "https://docs.google.com/forms/d/1fbyAzU_gAD4QNtZnLkDkFqw32OpUnFh1S6Ugs-LAw7E/formResponse";
                        HttpRequest mReq = new HttpRequest();
                        String col1 = pstTxt.getText().toString();
                        String data = "entry_41657080="
                                + URLEncoder.encode(col1);
                        String response = mReq.sendPost(fullUrl, data);
                        Log.i(myTag, response);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            });
            t.start();

    /*  }
    });*/

    return rootView;
}

} 它是这样工作的,但是当我播放按钮 findview 或 onclick 时,它让我得到这个:

java.lang.NullPointerException
at info.slidingmenu.CommunityFragment.onCreateView(CommunityFragment.java:25)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
at android.app.BackStackRecord.run(BackStackRecord.java:622)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
at android.app.FragmentManagerImpl.run(FragmentManager.java:417)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

改变这两行的顺序:

pstBtn = (Button) getView().findViewById(R.id.PostButton);
View rootView = inflater.inflate(R.layout.fragment_community, container, false);

到这个新订单,getView() 替换为您刚刚膨胀的视图 rootView

View rootView = inflater.inflate(R.layout.fragment_community, container, false);
pstBtn = (Button) rootView.findViewById(R.id.PostButton);

getView() 返回 null,因为片段视图尚未准备好(当您尝试调用 getView() 时,我们甚至还没有从 onCreateView 返回)。

假设您的按钮在 fragment_community.xml 布局上,您的 oncreateview 应该是这样的

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

        View rootView = inflater.inflate(R.layout.fragment_community,
                container, false);
        pstBtn = (Button) rootView.findViewById(R.id.PostButton);

        //i prefer to set listeners in onStart method but you can set it here also
        pstBtn.setOnClickListener(new OnClickListener());
        //....

       return rootView;
}