2017-02-24 4 views
0

フラグメントのOnClickイベント、トーストメッセージを表示しているときにうまく動作しますが、バックグラウンドタスクを実行しようとするとアプリケーションがクラッシュします。 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

関連する問題