我的应用程序有时会强制崩溃,以下是错误
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;
}
}
}
如果我没有将这部分添加到代码中,应用程序正常工作;
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();
}
}
});
我不知道崩溃的原因是什么,是doInbackground无法处理太多任务吗?非常感谢任何帮助
答案 0 :(得分:0)
sendEmail
的命名中出错, R.id.settings
为空
答案 1 :(得分: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 = "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();
}
}
});
同样在doInbackground中可以访问的活动内部声明message
,所以只需设置它:
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事件中只检查消息是否为空,然后只发送消息。