Good day, I am trying to execute my SincronizarCodigos class, when I swipe it executes synchronize to enter an AsyncTask and from there execute the method that would bring me data from the server, as I understand it enters Synchronize and calls SincronizarCodigos, but the context takes it as null. Is there any other way to execute it or how could I correct it, I have tried to remove the context, but doing so does not let me call the SynchronizeCodigos method later.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.formsaide, PID: 5510
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.<init>(Toast.java:111)
at android.widget.Toast.makeText(Toast.java:268)
at com.example.formsaide.MainActivityConsultaCodigos$Sincronizar.onPostExecute(MainActivityConsultaCodigos.java:161)
at com.example.formsaide.MainActivityConsultaCodigos$Sincronizar.onPostExecute(MainActivityConsultaCodigos.java:139)
at android.os.AsyncTask.finish(AsyncTask.java:679)
at android.os.AsyncTask.access$500(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:210)
at android.app.ActivityThread.main(ActivityThread.java:5943)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:817)
I/Process: Sending signal. PID: 5510 SIG: 9
Disconnected from the target VM, address: 'localhost:60142', transport: 'socket'
This is my code:
public class MainActivityConsultaCodigos extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConsultaCodigos();
//Referenciar AdaptadorCodigos
AdaptadorCodigos adapter = new AdaptadorCodigos(listaCodigos);
rvCodigos.setAdapter(adapter);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Sincronizar().execute();
swipe.setRefreshing(false);
}
});
}
public class Sincronizar extends AsyncTask<Void,Void,Void>{
Context context;
@Override
protected Void doInBackground(Void... voids) {
SincronizarCodigos(context);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
public void SincronizarCodigos(Context context){
if(checkConexion(this)){
ConexionSQLiteHelper conex = new ConexionSQLiteHelper(this,"bd_datos",null,1);
final SQLiteDatabase db = conn.getWritableDatabase();
StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://192.168.0.131/servicio_web/web/obtener_gastos.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for(int i = 0; i<array.length(); i++)
{
//JSONObject permite obtener un elemento dentro del JSONarray
JSONObject object = array.getJSONObject(i);
conex.GuardarCodigos(
object.getString("idOperario"),
object.getString("Operario"),
object.getString("bloque"),
object.getString("supervisor"),db);
}
} catch (JSONException e) {
e.printStackTrace();
}
ConsultaCodigos();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}
}
}
The problem seems to occur when trying to display a Toast but all due to the context used that has a null value.
onCreate()
You can initialize the context inside the your methodActivity
and use it in theAsyncTask
The value of the context is then used inside your
doInBackground()
del methodAsyncTask
: