我无法确定我错过了什么......
我有一个在后台运行的线程,doWork()
它监听一个等待新电子邮件的 gmail 帐户。当它收到一封新电子邮件时,我会显示一个对话框屏幕,其中包含打印或不打印电子邮件的选项,并且任何选项都会doWork()
再次调用该方法。
此外,当该方法doWork()
仍在监听后台运行的 gmail 帐户时,用户可以打印之前已经下载并插入数据库的电子邮件。打印后doWork()
再次调用该方法。
下面的例外情况会在打印电子邮件后立即发生,而它仍在收听 gmail 帐户。...
以下是我得到的例外情况:
04-01 16:39:52.365: I/System.out(22358): Subject :
04-01 16:39:55.705: D/dalvikvm(22358): GC_CONCURRENT freed 364K, 11% free 7106K/7943K, paused 3ms+3ms
04-01 16:39:56.400: E/get message(22358): java.lang.NullPointerException
04-01 16:39:56.400: W/dalvikvm(22358): threadid=11: thread exiting with uncaught exception (group=0x40c781f8)
04-01 16:39:56.405: E/AndroidRuntime(22358): FATAL EXCEPTION: Thread-2153
04-01 16:39:56.405: E/AndroidRuntime(22358): java.lang.NullPointerException
04-01 16:39:56.405: E/AndroidRuntime(22358): at com.example.gmailreceiver.MainActivity.retrieveEmails(MainActivity.java:578)
04-01 16:39:56.405: E/AndroidRuntime(22358): at com.example.gmailreceiver.MainActivity$6.run(MainActivity.java:312)
04-01 16:39:56.405: E/AndroidRuntime(22358): at java.lang.Thread.run(Thread.java:856)
04-01 16:40:00.290: I/System.out(22358): Total MSG: 4 unreaed: 4
这是一个执行的方法,直到在 Gmail 帐户中找到新电子邮件,然后它调用该行中的对话框dialogMessage();
:
public void doWork(){
try {
Thread.sleep(11000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
disableHotspot();
enableWifi();
while(!isOnline()){
runOnUiThread(new Runnable() {
@Override
public void run() {
tvStatus.setText("Waiting for a WiFi connection....");
tvStatus.setTextColor(Color.RED);
}
});
}
runOnUiThread(new Runnable() {
@Override
public void run() {
tvStatus.setText("Connected!");
tvStatus.setTextColor(Color.BLUE); }
});
boolean temp = false;
//while(!temp){
//retrieveEmails();
receiptPrint = new Thread(new Runnable() {
@Override
public void run() {
retrieveEmails();
runOnUiThread(new Runnable() {
@Override
public void run() {
if(newemail){
getOrderDB();
etOrder.setText(lOrder.get(0).toString());
dialogMessage();
}
}
});
}
});
receiptPrint.start();
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Log.e("doWork()", e.toString());
}
}
这是我的dialogMessage():
public void dialogMessage() {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_safe_bag);
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//dialog.setTitle(this.getString(R.string.alert_title_Confirm));
dialog.setCancelable(false);
// dialog.setOnDismissListener();
final Button btnOK = (Button) dialog.findViewById(R.id.dialog_btnOK);
final Button btnCancel = (Button) dialog.findViewById(R.id.dialog_btnCancel);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// getOrderDB();
EditText etOrderDialog = (EditText)dialog.findViewById(R.id.etorderDialog);
if(qtyOrder > 0){
etOrderDialog.setText(lOrder.get(0).toString());
}
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v)
{
Log.d("","Clicked on (Dialog) OK Button");
// TODO: create an Alert
//printing();
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
//Show your progress dialog in here
printingDialog = ProgressDialog.show(v.getContext(), "Printing...", "Please wait", true, false);
super.onPreExecute();
}
@Override
protected Void doInBackground( Void... params ) {
printing();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute( Void result ) {
//Dismiss your progress dialog here
printingDialog.dismiss();
doWork();
}
}.execute();
runOnUiThread(new Runnable() {
@Override
public void run() {
btnPrint.setEnabled(false);
}
});
dialog.dismiss();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
Log.d("","Clicked on (Dialog) Cancel Button");
dialog.cancel();
runOnUiThread(new Runnable() {
@Override
public void run() {
btnPrint.setEnabled(false);
}
});
doWorkForDialog();
}
});
dialog.show();
}