2015-07-10 70 views
0
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可以在片段內工作..爲什麼我的asynctask不能在片段內工作?

+2

移動'Toast.makeText(getActivity(),文本,Toast.LENGTH_LONG).show();''中onPostExecute' – Blackbelt

+0

1)。發佈你的錯誤/例外2)。做什麼@Blackbelt說。 –

回答

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(); 
    } 

} 
相關問題