Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从onClick执行后台任务时崩溃_Android - Fatal编程技术网

Android 从onClick执行后台任务时崩溃

Android 从onClick执行后台任务时崩溃,android,Android,片段上的OnClick事件,当我显示toast消息时,它工作正常,但当我试图执行后台任务时,我的应用程序崩溃。我在活动中使用了相同的代码,它工作得很好,如何在点击按钮时以片段形式启动后台任务 public class TestFragment extends Fragment implements Button.OnClickListener { private Button mButton; //Add at the top of the fragment EditText E

片段上的OnClick事件,当我显示toast消息时,它工作正常,但当我试图执行后台任务时,我的应用程序崩溃。我在活动中使用了相同的代码,它工作得很好,如何在点击按钮时以片段形式启动后台任务

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类BackgroundTestDriveTask扩展了AsyncTask{
拾取并放下ctx;
BackgroundTestDriveTask(View.OnClickListener ctx){
this.ctx=(pick_drop)ctx;
}
重写受保护的void onPreExecute(){
super.onPreExecute();
}
受保护的字符串doInBackground(字符串…参数){
字符串TestDrive_url=“torcentemotors.com/app/test_drive.php”;;
字符串方法=参数[0];
if(方法等于(“TestDrive_Submit”)){
字符串s_f_name=params[1];
字符串s_l_name=params[2];
字符串s_email=params[3];
字符串s_触点=参数[4];
字符串s_comment=params[5];
试一试{
URL URL=新URL(TestDrive\u URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
OutputStream OS=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(OS,“UTF-8”));
字符串数据=urlcoder.encode(“f_名称”,“UTF-8”)+“=”+urlcoder.encode(s_f_名称,“UTF-8”)+“&+urlcoder.encode(“l_名称”,“UTF-8”)+”+“+urlcoder.encode(“电子邮件”,“UTF-8”)+”=“+urlcoder.encode(s_电子邮件,“UTF-8”)+“&+urlcoder.encode(s_联系人,“UTF-8”)+”和“+URLCoder.encode”(“注释”,“UTF-8”)+”=“+URLCoder.encode(s_注释,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream=httpURLConnection.getInputStream();
IS.close();
回答“谢谢”;
}
}
公共类BackgroundTestDriveTask扩展了AsyncTask{
拾取并放下ctx;
BackgroundTestDriveTask(View.OnClickListener ctx){
this.ctx=(pick_drop)ctx;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串TestDrive_url=”http://torcentemotors.com/app/test_drive.php";
字符串方法=参数[0];
if(方法等于(“TestDrive_Submit”)){
字符串s_f_name=params[1];
字符串s_l_name=params[2];
字符串s_email=params[3];
字符串s_触点=参数[4];
字符串s_comment=params[5];
试一试{
URL URL=新URL(TestDrive\u URL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“POST”);
httpURLConnection.setDoOutput(true);
OutputStream OS=httpURLConnection.getOutputStream();
BufferedWriter BufferedWriter=新的BufferedWriter(新的OutputStreamWriter(OS,“UTF-8”));
字符串数据=URLEncoder.encode(“f_名称”、“UTF-8”)+”=“+URLEncoder.encode(s_f_名称,“UTF-8”)+”&”+
URLEncoder.encode(“l_名称”,“UTF-8”)+”=“+URLEncoder.encode(s_l_名称,“UTF-8”)+”&”+
URLEncoder.encode(“电子邮件”、“UTF-8”)+”=“+URLEncoder.encode(s_电子邮件,“UTF-8”)+”&”+
URLEncoder.encode(“触点”,“UTF-8”)+”=“+URLEncoder.encode(s_触点,“UTF-8”)+”&”+
URLEncoder.encode(“注释”,“UTF-8”)+“=”+URLEncoder.encode(s_注释,“UTF-8”);
bufferedWriter.write(数据);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream=httpURLConnection.getInputStream();
IS.close();
回答“谢谢”;
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
}
@凌驾
受保护的void onPostExecute(字符串结果){
//Toast.makeText(,result,Toast.LENGTH_LONG).show();
//Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
//super.onPostExecute(避免);
}

}

请添加崩溃的stackTrace.BackgroundTestDriveTask()用户getActivity()而不是此。“当我显示toast消息时,它工作正常,但当我尝试执行后台任务时,我的应用程序崩溃”显然,问题在于后台任务-您需要包含其代码-和错误日志。如果您最清楚,您可能还想仔细查看,您向我们询问的是什么!?请发布BackgroundTestDriveTask类的代码或至少提供该类的构造函数,@NickCardoso向好友致敬,我的Bac的“爱你”注释代码kgroundTestDriveTask任务。
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);
}