2017-02-24 49 views
0

OnClick Event on Fragment,當我顯示Toast消息時它的工作正常,但是當我試圖執行後臺任務時,我的應用程序崩潰。我在Activity中使用相同的代碼工作正常,如何在按鈕單擊時在Fragments中啓動後臺任務。從onClick執行後臺任務時崩潰

public class TestFragment extends Fragment implements Button.OnClickListener { 
    private Button mButton; //Add at the top of the fragment 
    EditText ET_input_first_name, ET_input_last_name, ET_input_email, ET_input_contact, ET_comments ; 
    String first_name, last_name, email , contact , comments; 

    public TestFragment() { 
    // Required empty public constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     //return inflater.inflate(R.layout.fragment_test, container, false); 
     // Inflate the layout for this fragment 
     //return inflater.inflate(R.layout.fragment_test, container, false); 

     View view = null; 
     view = inflater.inflate(R.layout.fragment_test, container, false); 

     ET_input_first_name = (EditText)view.findViewById(R.id.input_first_name); 
     ET_input_last_name = (EditText)view.findViewById(R.id.input_last_name); 
     ET_input_email = (EditText)view.findViewById(R.id.input_email); 
     ET_input_contact = (EditText)view.findViewById(R.id.input_contact); 
     ET_comments = (EditText)view.findViewById(R.id.comments); 

     //mButton = (Button) view.findViewById(R.id.btn_test_drive); 
     //mButton.setOnClickListener(this); 

     Button b = (Button) view.findViewById(R.id.btn_test_drive); 
     b.setOnClickListener(this); 

     return view; 
    } 

    @Override 
    public void onClick(View view) { 

     first_name = ET_input_first_name.getText().toString(); 
     last_name = ET_input_last_name.getText().toString(); 
     email  = ET_input_email.getText().toString(); 
     contact  = ET_input_contact.getText().toString(); 
     comments = ET_comments.getText().toString(); 

     String method = "TestDrive_Submit"; 
     BackgroundTestDriveTask backgroundTestDriveTask = new BackgroundTestDriveTask(this); 
     backgroundTestDriveTask.execute(method, first_name, last_name, email, contact, comments); 
     //Toast toast = Toast.makeText(getActivity(), "Submmit button Clicked! Again", Toast.LENGTH_SHORT); 
     //toast.show(); 
    } 


} 

以下是BackgroundTestDriveTask類

public class BackgroundTestDriveTask extends AsyncTask < String, Void, String > { 
    pick_drop ctx; 
    BackgroundTestDriveTask(View.OnClickListener ctx) { 
     this.ctx = (pick_drop) ctx; 
    } 
    Override protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    protected String doInBackground(String...params) { 
     String TestDrive_url = "torcentemotors.com/app/test_drive.php";; 
     String method = params[0]; 
     if (method.equals("TestDrive_Submit")) { 
     String s_f_name = params[1]; 
     String s_l_name = params[2]; 
     String s_email = params[3]; 
     String s_contact = params[4]; 
     String s_comment = params[5]; 
     try { 
      URL url = new URL(TestDrive_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 
      OutputStream OS = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
      String data = URLEncoder.encode("f_name", "UTF-8") + "=" + URLEncoder.encode(s_f_name, "UTF-8") + "&" + URLEncoder.encode("l_name", "UTF-8") + "=" + URLEncoder.encode(s_l_name, "UTF-8") + "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(s_email, "UTF-8") + "&" + URLEncoder.encode("contact", "UTF-8") + "=" + URLEncoder.encode(s_contact, "UTF-8") + "&" + URLEncoder.encode("comments", "UTF-8") + "=" + URLEncoder.encode(s_comment, "UTF-8"); 
      bufferedWriter.write(data); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      OS.close(); 
      InputStream IS = httpURLConnection.getInputStream(); 
      IS.close(); 
      return "ThankYou"; 
    } 
} 
+0

請添加崩潰的堆棧跟蹤。 –

+0

BackgroundTestDriveTask()用戶getActivity()而不是這個。 –

+0

這裏是我的後臺任務的代碼... – sanjay

回答

1
public class BackgroundTestDriveTask extends AsyncTask<String, Void, String > { 
pick_drop ctx; 
BackgroundTestDriveTask (View.OnClickListener ctx) { 
     this.ctx = (pick_drop) ctx; 
} 
@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
} 
@Override 
protected String doInBackground(String... params) { 
    String TestDrive_url = "http://torcentemotors.com/app/test_drive.php"; 
    String method = params[0]; 
    if(method.equals("TestDrive_Submit")) { 
     String s_f_name = params[1]; 
     String s_l_name = params[2]; 
     String s_email  = params[3]; 
     String s_contact = params[4]; 
     String s_comment = params[5]; 
     try { 
      URL url = new URL(TestDrive_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
      httpURLConnection.setRequestMethod("POST"); 
      httpURLConnection.setDoOutput(true); 
      OutputStream OS = httpURLConnection.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 

       String data = URLEncoder.encode("f_name", "UTF-8")+"="+URLEncoder.encode(s_f_name, "UTF-8")+"&"+ 
         URLEncoder.encode("l_name", "UTF-8")+"="+URLEncoder.encode(s_l_name, "UTF-8")+"&"+ 
         URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(s_email, "UTF-8")+"&"+ 
         URLEncoder.encode("contact", "UTF-8")+"="+URLEncoder.encode(s_contact, "UTF-8")+"&"+ 
         URLEncoder.encode("comments", "UTF-8")+"="+URLEncoder.encode(s_comment, "UTF-8"); 

       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       OS.close(); 

      InputStream IS = httpURLConnection.getInputStream(); 
      IS.close(); 
      return "ThankYou"; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 



    return null; 
} 

@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
} 

@Override 
protected void onPostExecute(String result) { 
    //Toast.makeText(,result, Toast.LENGTH_LONG).show(); 
    //Toast.makeText(ctx,result, Toast.LENGTH_LONG).show(); 
    //super.onPostExecute(aVoid); 
} 

}的代碼

+0

我的BackgroundTestDriveTask任務的代碼。 – sanjay

+1

非常感謝,它的工作。 – sanjay