2014-01-05 20 views
1

我的應用程序將迫使崩潰有時和以下是錯誤異步doInBackground會崩潰,並強制關閉

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 = "[email protected]"; 
         sendEmail(message, email); 
         } 

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

         String[] TO = {"[email protected]"}; 
         String[] CC = {"[email protected]"}; 
         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; 
     } 


    } 
     } 

如果我沒有將這部分添加到代碼中,應用程序正常工作;

sendEmail.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         String email = "[email protected]"; 
        sendEmail(message, email); 
        } 

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

        String[] TO = {"[email protected]"}; 
        String[] CC = {"[email protected]"}; 
        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(); 
        } 

       } 
       }); 

我不知道崩潰的原因是什麼,它doInbackground不能處理太多的任務?任何幫助是極大的讚賞

+0

看來sendEmail是空的,所以請確保R.id.settings在您的佈局中定義 –

+0

哦,是的!我犯了這個名字的一個小錯誤。這解決了這個問題,非常感謝! @mohamed_abdallah – user3021001

回答

0

sendEmail爲空,因爲你在R.id.settings

0

你不應該訪問的doInBackground UI元素(按鈕)命名犯了一個錯誤。

您應該在onCreate本身中設置onClickListener。

在活動的onCreate:

    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 = "[email protected]"; 
        sendEmail(message, email); 
        } 

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

        String[] TO = {"[email protected]"}; 
        String[] CC = {"[email protected]"}; 
        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活動中,這將是訪問內部doInbackground所以只設置:

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 + ". " ; 

現在唯一可能出現的問題將是,如果按鈕在設置消息值之前單擊。

因此,在onclick事件只是檢查消息是否爲空,然後只發送消息。