if and else if, java in android studio [duplicated]

1

I wanted to know how I can do so that if response is equal to id of Facebook, go to activity , and that otherwise go to another activity .

When I try with:

if (profil.getId().toString() == response)

and I run my application, I get a sign that the application was stopped. Could someone help me please?

Code:

  public class MainoRegistro extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maino_registro);

             final Profile profil = Profile.getCurrentProfile();
            RequestQueue queue = Volley.newRequestQueue(this);
            final String URL = "http://midominio.com/consulta.php?facebookid="+profil.getId().toString();
            StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    response = response.replaceAll("\"","").replace("]","").replace("[","");

            if (response == profil.getId()){
                Registrado();

            }else {
                registro();
            }


                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    LoginManager.getInstance().logOut();
                    IrPantallaLogin();
                }
            });
            queue.add(stringRequest);
        }

private void registro() {

    Intent intent = new Intent(MainoRegistro.this,Registro.class);
    finish();
    startActivity(intent);
}

private void Registrado() {
    Intent intent = new Intent(MainoRegistro.this,MainActivity.class);
    finish();
    startActivity(intent);

}

Now that I put the .trim () method the app stops and in the logcat it tells me the following:

  

java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.mathiastoledo.talkit / com.mathiastoledo.tal kit.MainoRegistro}:   java.lang.NullPointerException: Attempt to invoke virtual method   'java.lang.String com.facebook.Profile.getId ()' on a null object   reference

    
asked by Mathias Toledo 05.01.2017 в 22:31
source

1 answer

4

The visible problem seems to be the comparison of the two strings, you should not do it using the operator == :

if (profil.getId().toString() == response)

You must compare the strings using the equals() method and this way when they are equal, make a Intent to open the Activity you want, remember that all Activity must be registered in AndroidManifest.xml

The trim() method can be used to eliminate possible spaces.

To open another Activity you can do it using a Intent :

if (profil.getId().toString().equals(response.trim())){
        //Abre otra Activity.
        Intent myIntent = new Intent(Activity.this, OtraActivity.class);
        startActivity(myIntent);
}

The subsequent error:

  

java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.mathiastoledo.talkit / com.mathiastoledo.tal kit.MainoRegistro}:   java.lang.NullPointerException: Attempt to invoke virtual method   'java.lang.String com.facebook.Profile.getId ()' on a null object   reference

is generated since the variable profil has a value null .

    
answered by 05.01.2017 / 22:48
source