Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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
Java 异步doInBackground将崩溃,并强制关闭_Java_Android_Json_Eclipse_Android Asynctask - Fatal编程技术网

Java 异步doInBackground将崩溃,并强制关闭

Java 异步doInBackground将崩溃,并强制关闭,java,android,json,eclipse,android-asynctask,Java,Android,Json,Eclipse,Android Asynctask,我的应用程序有时会强制崩溃,下面是错误 01-05 14:55:40.341: E/AndroidRuntime(16526): FATAL EXCEPTION: AsyncTask #3 01-05 14:55:40.341: E/AndroidRuntime(16526): java.lang.RuntimeException: An error occured while executing doInBackground() 01-05 14:55:40.

我的应用程序有时会强制崩溃,下面是错误

    01-05 14:55:40.341: E/AndroidRuntime(16526): FATAL EXCEPTION: AsyncTask #3
    01-05 14:55:40.341: E/AndroidRuntime(16526): java.lang.RuntimeException: An error 
    occured while executing doInBackground()
    01-05 14:55:40.341: E/AndroidRuntime(16526): at   
    android.os.AsyncTask$3.done(AsyncTask.java:299)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at 
    java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at
    java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at  
    java.util.concurrent.FutureTask.run(FutureTask.java:239)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at  
    android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at 
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at  
    java.lang.Thread.run(Thread.java:841)
    01-05 14:55:40.341: E/AndroidRuntime(16526): Caused by: 
    java.lang.NullPointerException
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at   

    com.example.splashscreentwo.EmployeePayslip$LoadAllPayslip.doInBackground
    (EmployeePayslip.java:152)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at  
    com.example.splashscreentwo.EmployeePayslip$LoadAllPayslip.doInBackground
    (EmployeePayslip.java:1)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at  
    android.os.AsyncTask$2.call(AsyncTask.java:287)
    01-05 14:55:40.341: E/AndroidRuntime(16526):    at 
    java.util.concurrent.FutureTask.run(FutureTask.java:234)
这是页面的以下代码

    package com.example.splashscreentwo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class EmployeePayslip extends Activity {

    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    JSONArray payslip = null;
    ArrayList<HashMap<String, String>> payslipList;

    // url to get all fulltime employees list
    private static String url_payslip = "http://rollit.sg/FYP/ExportPayslip.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PAYSLIP = "payslip";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";
    private static final String TAG_PAYSLIPNO = "payslipno";
    private static final String TAG_NETSALARY = "netsalary";
    private static final String TAG_ISSUEDATE = "issuedate";
    private static final String TAG_STARTOFPAYSLIP = "startofpayslip";
    private static final String TAG_ENDOFPAYSLIP = "endofpayslip";
    private static final String TAG_TYPEOFALLOWANCE = "typeofallowance";
    private static final String TAG_ALLOWANCEAMT = "allowanceamt";
    private static final String TAG_ALLOWANCEDATE = "allowancedate";
    private static final String TAG_AVAILABLEALLOWANCE = "availableallowance";
    private static final String TAG_TYPEOFDEDUCTION = "typeofdeduction";
    private static final String TAG_DEDUCTIONAMT = "deductionamt";
    private static final String TAG_DEDUCTIONDATE = "deductiondate";
    private static final String TAG_AGREEDOVERTIMERATE = "agreedovertimerate";
    private static final String TAG_OVERTIMERATE = "overtimerate";
    private static final String TAG_STARTOFOVERTIMEPERIOD = "startofovertimeperiod";
    private static final String TAG_ENDOFOVERTIMEPERIOD = "endofovertimeperiod";
    private static final String TAG_BASICSALARY = "basicsalary";
    private static final String TAG_EXTRAPAYMENT = "extrapayment";




    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.employeepayslip);




     payslipList = new ArrayList<HashMap<String, String>>();

     // Loading all fulltime employees in Background Thread
     new LoadAllPayslip().execute();


    }

    class LoadAllPayslip extends AsyncTask<String, String, String> {

       protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_payslip, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // fulltime employees found
                    // Getting Array of fulltime employees
                    payslip = json.getJSONArray(TAG_PAYSLIP);

                    // looping through All fulltime employees
                    for (int i = 0; i < payslip.length(); i++) {
                        JSONObject c = payslip.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        final String name = c.getString(TAG_NAME);
                        final String payslipno = c.getString(TAG_PAYSLIPNO);
                        final String netsalary = c.getString(TAG_NETSALARY);
                        final String issuedate = c.getString(TAG_ISSUEDATE);
                        final String startofpayslip = c.getString(TAG_STARTOFPAYSLIP);
                        final String endofpayslip = c.getString(TAG_ENDOFPAYSLIP);
                        final String typeofallowance = c.getString(TAG_TYPEOFALLOWANCE);
                        final String allowanceamt = c.getString(TAG_ALLOWANCEAMT);
                        final String allowancedate = c.getString(TAG_ALLOWANCEDATE);
                        final String availableallowance = c.getString(TAG_AVAILABLEALLOWANCE);
                        final String typeofdeduction = c.getString(TAG_TYPEOFDEDUCTION);
                        final String deductionamt = c.getString(TAG_DEDUCTIONAMT);
                        final String deductiondate = c.getString(TAG_DEDUCTIONDATE);
                        final String agreedovertimerate = c.getString(TAG_AGREEDOVERTIMERATE);
                        final String overtimerate = c.getString(TAG_OVERTIMERATE);
                        final String startofovertimeperiod = c.getString(TAG_STARTOFOVERTIMEPERIOD);
                        final String endofovertimeperiod = c.getString(TAG_ENDOFOVERTIMEPERIOD);
                        final String basicsalary = c.getString(TAG_BASICSALARY);
                        final String extrapayment = c.getString(TAG_EXTRAPAYMENT);

                        ImageButton sendSMS= (ImageButton) findViewById(R.id.send);
                        Button sendEmail = (Button) findViewById(R.id.settings);

                        final String message = "Dear " + name + ", your payslip number is " + payslipno +" and your net salary for the period " + startofpayslip + " till " + 
                                  endofpayslip + " is "+  netsalary + " and it is issused on " + issuedate + ". According to the agreed overtime rate of " + agreedovertimerate + " from " + 
                                            startofovertimeperiod + " till " + endofovertimeperiod +  ", your net salary is calculated by adding your basic salary of " +
                                            basicsalary + " to your available allowance of " + typeofallowance + " of " + allowanceamt + " on " + allowancedate + ", and any other extra payment of" +
                                            extrapayment + " and deducting from " + typeofdeduction 
                                            + " of " + deductionamt + " on " + deductiondate + ". Your availabe allowance for this month is " + availableallowance + ". " ;
                       sendSMS.setOnClickListener(new View.OnClickListener() {

                              @Override
                              public void onClick(View view) {

                                String phoneNo = "96545373";
                                sendSMS(phoneNo, message);     

                              }

                            private void sendSMS(String phoneNo, String message) {

                                SmsManager sms = SmsManager.getDefault();
                              ArrayList<String> parts = sms.divideMessage(message);
                            sms.sendMultipartTextMessage(phoneNo, null, parts, null, null); 

                            }
                          });
                     // creating new HashMap
                    sendEmail.setOnClickListener(new View.OnClickListener() {
                         public void onClick(View view) {
                             String email = "oslhanzo@hotmail.com";
                         sendEmail(message, email);
                      }

                    private void sendEmail(String message, String email) {
                        Log.i("Send email", "");

                        String[] TO = {"oslhanzo@hotmail.com"};
                        String[] CC = {"mcmohd@gmail.com"};
                        Intent emailIntent = new Intent(Intent.ACTION_SEND);
                        emailIntent.setData(Uri.parse("mailto:"));
                        emailIntent.setType("text/plain");


                        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
                        emailIntent.putExtra(Intent.EXTRA_CC, CC);
                        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Payslip for period " + startofpayslip + " till " + endofpayslip);
                        emailIntent.putExtra(Intent.EXTRA_TEXT, message);

                        try {
                           startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                           Log.i("Finished sending email...", "");
                        } catch (android.content.ActivityNotFoundException ex) {
                           Toast.makeText(EmployeePayslip.this, 
                           "There is no email client installed.", Toast.LENGTH_SHORT).show();
                        }

                     }
                    });

                    }
                } else {
                    // no fulltime employee found
                    // Launch Add New employee Activity
                 //   Intent i = new Intent(getApplicationContext(),
                    //        NewProductActivity.class);
                    // Closing all previous activities
                 //   i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  //  startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


    }
         }

我不知道坠机的原因是什么,是不是在后台无法处理太多的任务?非常感谢您提供的任何帮助

sendmail
无效,因为您在命名
R.id.settings
时出错。您不应访问doInBackground中的UI元素(按钮)

您应该只在onCreate中设置onClickListener

在创建活动时:

                  ImageButton sendSMS= (ImageButton) findViewById(R.id.send);
                    Button sendEmail = (Button) findViewById(R.id.settings);


                   sendSMS.setOnClickListener(new View.OnClickListener() {

                          @Override
                          public void onClick(View view) {

                            String phoneNo = "96545373";
                            sendSMS(phoneNo, message);     

                          }

                        private void sendSMS(String phoneNo, String message) {

                            SmsManager sms = SmsManager.getDefault();
                          ArrayList<String> parts = sms.divideMessage(message);
                        sms.sendMultipartTextMessage(phoneNo, null, parts, null, null); 

                        }
                      });
                 // creating new HashMap
                sendEmail.setOnClickListener(new View.OnClickListener() {
                     public void onClick(View view) {
                         String email = "oslhanzo@hotmail.com";
                     sendEmail(message, email);
                  }

                private void sendEmail(String message, String email) {
                    Log.i("Send email", "");

                    String[] TO = {"oslhanzo@hotmail.com"};
                    String[] CC = {"mcmohd@gmail.com"};
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.setData(Uri.parse("mailto:"));
                    emailIntent.setType("text/plain");


                    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
                    emailIntent.putExtra(Intent.EXTRA_CC, CC);
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Payslip for period " + startofpayslip + " till " + endofpayslip);
                    emailIntent.putExtra(Intent.EXTRA_TEXT, message);

                    try {
                       startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                       Log.i("Finished sending email...", "");
                    } catch (android.content.ActivityNotFoundException ex) {
                       Toast.makeText(EmployeePayslip.this, 
                       "There is no email client installed.", Toast.LENGTH_SHORT).show();
                    }

                 }
                });
现在唯一可能的问题是在设置消息值之前单击按钮


因此,在onclick事件中,只需检查消息是否不为null,然后只发送消息。

sendEmail似乎为null,因此请确保在布局中定义了R.id.settings。是!我把名字弄错了一点。这就解决了问题,谢谢你,伙计@穆罕默德·阿卜杜拉
                  ImageButton sendSMS= (ImageButton) findViewById(R.id.send);
                    Button sendEmail = (Button) findViewById(R.id.settings);


                   sendSMS.setOnClickListener(new View.OnClickListener() {

                          @Override
                          public void onClick(View view) {

                            String phoneNo = "96545373";
                            sendSMS(phoneNo, message);     

                          }

                        private void sendSMS(String phoneNo, String message) {

                            SmsManager sms = SmsManager.getDefault();
                          ArrayList<String> parts = sms.divideMessage(message);
                        sms.sendMultipartTextMessage(phoneNo, null, parts, null, null); 

                        }
                      });
                 // creating new HashMap
                sendEmail.setOnClickListener(new View.OnClickListener() {
                     public void onClick(View view) {
                         String email = "oslhanzo@hotmail.com";
                     sendEmail(message, email);
                  }

                private void sendEmail(String message, String email) {
                    Log.i("Send email", "");

                    String[] TO = {"oslhanzo@hotmail.com"};
                    String[] CC = {"mcmohd@gmail.com"};
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);
                    emailIntent.setData(Uri.parse("mailto:"));
                    emailIntent.setType("text/plain");


                    emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
                    emailIntent.putExtra(Intent.EXTRA_CC, CC);
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Payslip for period " + startofpayslip + " till " + endofpayslip);
                    emailIntent.putExtra(Intent.EXTRA_TEXT, message);

                    try {
                       startActivity(Intent.createChooser(emailIntent, "Send mail..."));
                       Log.i("Finished sending email...", "");
                    } catch (android.content.ActivityNotFoundException ex) {
                       Toast.makeText(EmployeePayslip.this, 
                       "There is no email client installed.", Toast.LENGTH_SHORT).show();
                    }

                 }
                });
 message = "Dear " + name + ", your payslip number is " + payslipno +" and your net salary for the period " + startofpayslip + " till " + 
                                  endofpayslip + " is "+  netsalary + " and it is issused on " + issuedate + ". According to the agreed overtime rate of " + agreedovertimerate + " from " + 
                                            startofovertimeperiod + " till " + endofovertimeperiod +  ", your net salary is calculated by adding your basic salary of " +
                                            basicsalary + " to your available allowance of " + typeofallowance + " of " + allowanceamt + " on " + allowancedate + ", and any other extra payment of" +
                                            extrapayment + " and deducting from " + typeofdeduction 
                                            + " of " + deductionamt + " on " + deductiondate + ". Your availabe allowance for this month is " + availableallowance + ". " ;