为什么我的asynctask不能在片段内工作?

时间:2015-07-10 19:56:34

标签: android

public class MyFragment extends Fragment implements OnClickListener {
    EditText et;
    Button submit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
                R.layout.MyFragment, container, false);
        et = (EditText) mLinearLayout.findViewById(R.id.etet);
        submit = (Button) mLinearLayout.findViewById(R.id.bsubmit);
        submit.setOnClickListener(this);
        return mLinearLayout;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bsubmit:
              new processet().execute();
            break;
        }
    }

    private class processet extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected JSONObject doInBackground(String... args) {
            String text = et.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = null;
            Toast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {

        }

    }
}

为什么这段代码不起作用? 我已经检查过按钮onClickListener工作正常.. 这只是一个简单的代码,我不知道它为什么不工作.. 我认为必须进行一些更改,以便ASyncTask可以在片段内工作..

1 个答案:

答案 0 :(得分:1)

private class processet extends AsyncTask<Void, Void, JSONObject> {

    private ProgressDialog pDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Show pDialog
    }

    @Override
    protected JSONObject doInBackground(Void... args) {
        String text = et.getText().toString();
        UserFunctions userFunction = new UserFunctions();


        return new jsonObject();
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        //dismiss pDialog;
        Toast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
    }

}