我想知道为什么调用方法后全局String变量为null。
我的代码是:
public void login_fb()
{
final Funciones f = (Funciones) getApplication();
fbloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
@Override
public void onSuccess(LoginResult loginResult)
{
GraphRequest graphRequest = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
@Override
public void onCompleted(JSONObject object, GraphResponse response)
{
try
{
correofb = object.getString("email");
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
Bundle parametros = new Bundle();
parametros.putString("fields","email");
graphRequest.setParameters(parametros);
graphRequest.executeAsync();
Intent intent = new Intent(LoginTienda.this,Comprador_registrado.class);
startActivity(intent);
}
@Override
public void onCancel()
{
}
@Override
public void onError(FacebookException error)
{
String msgerror = error.getMessage();
Toast.makeText(LoginTienda.this, msgerror, Toast.LENGTH_SHORT).show();
}
});
profileTracker = new ProfileTracker()
{
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile)
{
if(currentProfile == null)
{
f.borrar_sp();
Intent i = new Intent(LoginTienda.this,MainActivity.class);
startActivity(i);
}
else
{
existe_correo_fb();
Toast.makeText(getApplicationContext(),usr_correo_existe,Toast.LENGTH_LONG).show(); //the value of usr_correo is null here
//Toast.makeText(getApplicationContext(),correofb,Toast.LENGTH_LONG).show();
f.insertar_sp("usrcorreo",correofb);
f.insertar_sp("usrnombre",nombrefb);
f.insertar_sp("usrapellido",apellidofb);
f.insertar_sp("reg","fb");
}
}
};
}
从login_fb()我调用existe_correo_fb()。两种方法都使用名为usr_correo_existe的全局变量,该变量在活动开始时声明。
usr_correo_existe的值在existe_correo_fb()中指定。从这个方法中,usr_correo_existe的值是正确的,但是如果我在login_fb()方法中检查相同的值,则该值为null。而且我不知道为什么。我认为该值应与其他方法相同,因为usr_correo_existe是一个全局变量。
existe_correo_fb()方法的代码:
public void existe_correo_fb()
{
//Toast.makeText(getApplicationContext(),correofb,Toast.LENGTH_LONG).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.CORREO_RS_URL,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
usr_correo_existe = response; //the value of usr_correo_existe is assigned and right here
Toast.makeText(getApplicationContext(),usr_correo_existe,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError Error)
{
Snackbar.make(findViewById(android.R.id.content), "No se ha podido hacer la conexión", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}){
@Override
protected Map<String,String> getParams() throws AuthFailureError
{
Map<String,String> params = new HashMap<>();
params.put("correo",correofb);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(LoginTienda.this);
requestQueue.add(stringRequest);
}
usr_correo_existe的声明:
public class LoginTienda extends AppCompatActivity
{
private String usr_correo_existe;
}
如何查看usr_correo_existe变量的正确值?
感谢。