片段上的OnClick事件,当我显示Toast消息时它工作正常,但当我尝试执行后台任务时,我的应用程序崩溃了。我在Activity中使用了相同的代码,它的工作正常,如何在按钮单击时在Fragments中启动后台任务。
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 :(得分: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);
}
}