logcat的:
致命异常:IntentService [BMI] 处理:user.com.test2,PID:3377 java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.Object.toString()'在null对象引用上 at services.SendBpRecordServer.onHandleIntent(SendBpRecordServer.java:66) 在android.app.IntentService $ ServiceHandler.handleMessage(IntentService.java:66) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.os.HandlerThread.run(HandlerThread.java:61)
我要做的是:
我使用凌空从服务器获取数据,并使用IntentService
在后台进行操作。并且还使用ResultReceiver
将数据从服务获取到活动。
UpdateData.class
public class Updatedata extends AppCompatActivity implements MyResultReceiver.Receiver{
DbHelper dbHelper;
public MyResultReceiver resultReceiver, resultReceiverBMI, resultReceiverBP;
ProgressDialog progressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notify);
Log.i("On Creste","Yes");
dbHelper=new DbHelper(this);
//before starting further empty all the tables
dbHelper.emptyTables();
progressDialog=new ProgressDialog(Updatedata.this);
progressDialog.setTitle("Fetching Profile Data");
progressDialog.setMessage("Please Wait...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
resultReceiver=new MyResultReceiver(new Handler());
resultReceiver.setReceiver(this);
System.out.println("Fetch Diabetes Data Service "+ SendDiabetesRecordServer.TAG);
Intent intent=new Intent(this, SendDiabetesRecordServer.class);
intent.putExtra("nameTag","fetch");
intent.putExtra("receiverTag",resultReceiver);
startService(intent);
Log.i("Fetch BMI Data ", SendBmiRecordServer.TAG);
resultReceiverBMI=new MyResultReceiver(new Handler());
resultReceiverBMI.setReceiver(this);
Intent intent1=new Intent(this,SendBmiRecordServer.class);
intent1.putExtra("nameTag","fetch");
intent1.putExtra("receiverTagBMI",resultReceiverBMI);
startService(intent1);
Log.i("Fetch BP Data ", SendBpRecordServer.TAG);
resultReceiverBP=new MyResultReceiver(new Handler());
resultReceiverBP.setReceiver(this);
Intent intent2=new Intent(this,SendBpRecordServer.class);
intent2.putExtra("nameTag","fetch");
intent2.putExtra("receiverTagBP",resultReceiverBP);
startService(intent2);
/*if (progressDialog.isShowing()) {
progressDialog.dismiss();
}*/
}
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
// TODO Auto-generated method stub
Log.d("Diabetes","received result from Service= "+resultData.getString("ServiceTag"));
Log.d("BMI ","received result from Service= "+resultData.getString("ServiceTag"));
/* if (progressDialog.isShowing()) {
progressDialog.dismiss();
}*/
Log.i("Get Values ",Integer.toString(Constants.getValues));
if (resultCode==0 && resultData.getBoolean("Process_Complete"))
Constants.getValues++;
if (resultCode==1 && resultData.getBoolean("Process_Complete_bmi"))
Constants.getValues++;
if (resultCode==2 && resultData.getBoolean("Process_Complete_bp"))
Constants.getValues++;
if (Constants.getValues==3)
{
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
startActivity(new Intent(this,MainActivity.class));
finish();
}
}
}
IntentService
模板:
@Override
protected void onHandleIntent(Intent intent) {
String recName=intent.getStringExtra("nameTag");
if (recName.equals("fetch"))
{
fetchDatFromServer();
ResultReceiver resultReceiver=intent.getParcelableExtra("receiverTag");
Log.d("diabetes Reseult ",resultReceiver.toString());
Bundle b= new Bundle();
b.putBoolean("Process_Complete",true);
resultReceiver.send(0, b);
}
对于3 IntentService
,这是相同的。
获取错误: -
@Override
protected void onHandleIntent(Intent intent) {
String recName=intent.getStringExtra("nameTag");
if (recName.equals("fetch"))
{
Log.i("Send Data","Yes");
fetchDatFromServer();
ResultReceiver resultReceiver=intent.getParcelableExtra("receiverTag");
**Log.d("BP Reseult ",resultReceiver.toString());** //Getting Error at this line
Bundle bun= new Bundle();
bun.putBoolean("Process_Complete_bp",true);
resultReceiver.send(2, bun);
}
调试:
在第二个意图类中,即SendBmiRecordServer.class
我非常确定这个问题只出在这堂课上。
因为当我把Log.i("Get Values", Constants.getValues);
我已在Constants.getValues=0
Constants.class
我得到的结果直到1
答案 0 :(得分:0)
试试这个
覆盖onDestroy方法并执行以下操作
@Override
public void onDestroy() {
super.onDestroy();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}