按钮在片段中失败

时间:2015-01-18 00:50:39

标签: java android android-fragments

我在片段类中定义了一个按钮和一个编辑文本,但是当我通过按主活动的列表视图启动它时,它崩溃了,我尝试将编辑文本放在postData中它仍然有用,仍然我需要让按钮工作,我不能切换它的位置,因为我需要点击它,无论如何,我尝试删除findviewbyid,只是把onclicklistiner但它也得到了我相同的Nullexcpection,所以我想它可能是一些东西从碎片它自己。

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$1.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)

2 个答案:

答案 0 :(得分:0)

更改这两行的顺序:

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,因为Fragment视图尚未准备就绪(当您尝试调用onCreateView时,我们甚至没有从getView()返回。)

答案 1 :(得分:0)

假设您的按钮位于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;
}