I have this AsynTask class that works perfectly on an activity. I used a similar method in a new app that uses fragments because of tabs and it's failing me.
Below is the context menu item where I instantiate the class (OverviewFragment.java):
@Override
public boolean onContextItemSelected(MenuItem item){
//AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//int position = info.position;
super.onContextItemSelected(item);
if(item.getTitle() == "Show Report"){
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + this.getPDFFileName() + ".pdf");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
}
if(item.getTitle()=="Upload report via webservice")
{
itemClicked = "uploadMain_file";
uploadToERP uploadToERP = new uploadToERP(this.getActivity());
uploadToERP.execute();
}
if(item.getTitle()=="Send as attachment")
{
this.sendMail(this.getPDFFileName());
}
if(item.getTitle()=="Upload to ERP")
{
itemClicked = "uploadMain_data";
uploadToERP uploadToERP = new uploadToERP(getActivity());
uploadToERP.execute();
}
return true;
}
And here is the class:
package com.application.sweetiean.stlmaintenance;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import com.application.sweetiean.stlservicing.Serv_OverviewFragment;
import com.application.sweetiean.stlservicing.ServicingActivity;
/**
* Created by sweetiean on 1/15/2016.
*/
public class uploadToERP extends AsyncTask<String, Integer, Void> {
Context context;
String file;
String Maintenance_Info;
String Task;
String Service_Info;
String Servicing;
String Replacement;
public uploadToERP(Context c){
context = c;
}
@Override
protected Void doInBackground(String... params) {
Boolean doNothing = false;
try{
if(OverviewFragment.itemClicked.equals("uploadMain_file")) {
file = MaintenanceActivity.Main_axcall.SendMainFile(OverviewFragment.getBytesFromFile(), OverviewFragment.sysaidIdFileName + ".pdf");
doNothing = true;
}
/*if(Serv_OverviewFragment.itemClicked.equals("uploadServ_file")) {
file = ServicingActivity.Serv_axcall.SendServFile(Serv_OverviewFragment.getBytesFromFile(),Serv_OverviewFragment.sysaidIdFileName+".pdf");
doNothing = true;
}*/
if(!doNothing) {
//check info because you will be receiving from both info data
MaintenanceAppDB getRecord = new MaintenanceAppDB(context);
//since there are two modules using this class I will have to set itemclicked for all the context
//menu items and use that check
if(OverviewFragment.itemClicked.equals("uploadMain_data")) {
Maintenance_Info = getRecord.getMaintenanceInfoRecord(OverviewFragment.sysaidIdFileName);
Task = getRecord.getTaskRecord(OverviewFragment.sysaidIdFileName);
}
/*if(Serv_OverviewFragment.itemClicked.equals("uploadServ_data")) {
Service_Info = getRecord.getServiceInfoRecord(Serv_OverviewFragment.sysaidIdFileName);
Servicing = getRecord.getServicingRecord(Serv_OverviewFragment.sysaidIdFileName);
Replacement = getRecord.getReplacementRecord(Serv_OverviewFragment.sysaidIdFileName);
}*/
}//end insert
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if(OverviewFragment.itemClicked.equals("uploadMain_file")) {
Toast.makeText(context, file, Toast.LENGTH_SHORT).show();
}/*else if(Serv_OverviewFragment.itemClicked.equals("uploadServ_file")) {
Toast.makeText(context, file, Toast.LENGTH_SHORT).show();
}*/else if(OverviewFragment.itemClicked.equals("uploadMain_data")) {
Toast.makeText(context, Maintenance_Info, Toast.LENGTH_SHORT).show();
Toast.makeText(context, Task, Toast.LENGTH_SHORT).show();
}/*else if(Serv_OverviewFragment.itemClicked.equals("uploadServ_data")) {
Toast.makeText(context, Service_Info, Toast.LENGTH_SHORT).show();
Toast.makeText(context, Servicing, Toast.LENGTH_SHORT).show();
Toast.makeText(context, Replacement, Toast.LENGTH_SHORT).show();
}*/
Toast.makeText(context, "Upload to ERP complete!!!", Toast.LENGTH_LONG).show();
}
}
Any help will be deeply appreciated. I have read many other answers since last Friday and I still can't find a solution to my problem. The onPostExecute method works alright but the doInBackground method does not get called at all. I realised that when I was debugging.
After some much-needed motivation from Gavriel and David, I finally got the debugger to run through the doInbackground method. I commented out the second If statement as that particular class is not loaded at the time when the AsyncTask class is called(makes sense) Thank you David for making me look harder anyway so Gavriel, I finally got the debugger to run through that method and here is the error code I am getting now SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Value cannot be null.
Parameter name: This method cannot be invoked because a required argument has not been supplied.' faultactor: 'null' detail: org.kxml2.kdom.Node@41e9cc30
on the line Maintenance_Info = getRecord.getMaintenanceInfoRecord(OverviewFragment.sysaidIdFileName);
it's totally unrelated to my initial problem. I have to look to the methods in my db class and the webservice.java to figure out which required argument is returning null