If else statement in new Intent [duplicate]









up vote
0
down vote

favorite













This question already has an answer here:



  • How do I compare strings in Java?

    23 answers



My application is working fine until I add if statement in the new intent the application suddenly closing. I would like to go to the new intent base on what radio button is selected.



 @Override
public void onClick(View view)
int RadioId = radioGroupB.getCheckedRadioButtonId();
radioButton = findViewById(RadioId);

if (radioButton.getText()=="Circle")
Intent intenta=new Intent(getApplicationContext(),SecAct.class);
intenta.putExtra ( "1", editText.getText().toString());
intenta.putExtra("2", editText2.getText().toString());
startActivity(intenta);
if (radioButton.getText()=="Square")
Intent intent=new Intent(getApplicationContext(),square.class);
intent.putExtra ( "1", editText.getText().toString());
intent.putExtra("2", editText2.getText().toString());
startActivity(intent);




Here's what it looks like when I add if statement.










share|improve this question













marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    If by "the application suddenly closing" you mean it's crashing, you'll need to look at the stack trace to determine the cause of the crash.
    – Mike M.
    2 days ago







  • 1




    Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
    – HaroldSer
    2 days ago










  • "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87)" it says at the logcat sir
    – Skiwmish
    2 days ago














up vote
0
down vote

favorite













This question already has an answer here:



  • How do I compare strings in Java?

    23 answers



My application is working fine until I add if statement in the new intent the application suddenly closing. I would like to go to the new intent base on what radio button is selected.



 @Override
public void onClick(View view)
int RadioId = radioGroupB.getCheckedRadioButtonId();
radioButton = findViewById(RadioId);

if (radioButton.getText()=="Circle")
Intent intenta=new Intent(getApplicationContext(),SecAct.class);
intenta.putExtra ( "1", editText.getText().toString());
intenta.putExtra("2", editText2.getText().toString());
startActivity(intenta);
if (radioButton.getText()=="Square")
Intent intent=new Intent(getApplicationContext(),square.class);
intent.putExtra ( "1", editText.getText().toString());
intent.putExtra("2", editText2.getText().toString());
startActivity(intent);




Here's what it looks like when I add if statement.










share|improve this question













marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    If by "the application suddenly closing" you mean it's crashing, you'll need to look at the stack trace to determine the cause of the crash.
    – Mike M.
    2 days ago







  • 1




    Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
    – HaroldSer
    2 days ago










  • "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87)" it says at the logcat sir
    – Skiwmish
    2 days ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • How do I compare strings in Java?

    23 answers



My application is working fine until I add if statement in the new intent the application suddenly closing. I would like to go to the new intent base on what radio button is selected.



 @Override
public void onClick(View view)
int RadioId = radioGroupB.getCheckedRadioButtonId();
radioButton = findViewById(RadioId);

if (radioButton.getText()=="Circle")
Intent intenta=new Intent(getApplicationContext(),SecAct.class);
intenta.putExtra ( "1", editText.getText().toString());
intenta.putExtra("2", editText2.getText().toString());
startActivity(intenta);
if (radioButton.getText()=="Square")
Intent intent=new Intent(getApplicationContext(),square.class);
intent.putExtra ( "1", editText.getText().toString());
intent.putExtra("2", editText2.getText().toString());
startActivity(intent);




Here's what it looks like when I add if statement.










share|improve this question














This question already has an answer here:



  • How do I compare strings in Java?

    23 answers



My application is working fine until I add if statement in the new intent the application suddenly closing. I would like to go to the new intent base on what radio button is selected.



 @Override
public void onClick(View view)
int RadioId = radioGroupB.getCheckedRadioButtonId();
radioButton = findViewById(RadioId);

if (radioButton.getText()=="Circle")
Intent intenta=new Intent(getApplicationContext(),SecAct.class);
intenta.putExtra ( "1", editText.getText().toString());
intenta.putExtra("2", editText2.getText().toString());
startActivity(intenta);
if (radioButton.getText()=="Square")
Intent intent=new Intent(getApplicationContext(),square.class);
intent.putExtra ( "1", editText.getText().toString());
intent.putExtra("2", editText2.getText().toString());
startActivity(intent);




Here's what it looks like when I add if statement.





This question already has an answer here:



  • How do I compare strings in Java?

    23 answers







java android if-statement






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Skiwmish

135




135




marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Andy Turner java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
2 days ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    If by "the application suddenly closing" you mean it's crashing, you'll need to look at the stack trace to determine the cause of the crash.
    – Mike M.
    2 days ago







  • 1




    Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
    – HaroldSer
    2 days ago










  • "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87)" it says at the logcat sir
    – Skiwmish
    2 days ago












  • 1




    If by "the application suddenly closing" you mean it's crashing, you'll need to look at the stack trace to determine the cause of the crash.
    – Mike M.
    2 days ago







  • 1




    Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
    – HaroldSer
    2 days ago










  • "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87)" it says at the logcat sir
    – Skiwmish
    2 days ago







1




1




If by "the application suddenly closing" you mean it's crashing, you'll need to look at the stack trace to determine the cause of the crash.
– Mike M.
2 days ago





If by "the application suddenly closing" you mean it's crashing, you'll need to look at the stack trace to determine the cause of the crash.
– Mike M.
2 days ago





1




1




Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
2 days ago




Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
2 days ago












"java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87)" it says at the logcat sir
– Skiwmish
2 days ago




"java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87)" it says at the logcat sir
– Skiwmish
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Looks like radioButton.getText() returns a String. You can't use == to compare Strings use .equals() instead.



Change it to :



 if ("Circle".equals(radioButton.getText()) {





share|improve this answer




















  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
    – Skiwmish
    2 days ago










  • Looks like radioButton is null.
    – Nicholas K
    2 days ago










  • @NicholasK i think you are missing .toString() after getText()
    – Saurabh Bhandari
    2 days ago










  • That won't result in a NPE.
    – Nicholas K
    2 days ago










  • @NicholasK how to make radioButton not null?
    – Skiwmish
    2 days ago

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Looks like radioButton.getText() returns a String. You can't use == to compare Strings use .equals() instead.



Change it to :



 if ("Circle".equals(radioButton.getText()) {





share|improve this answer




















  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
    – Skiwmish
    2 days ago










  • Looks like radioButton is null.
    – Nicholas K
    2 days ago










  • @NicholasK i think you are missing .toString() after getText()
    – Saurabh Bhandari
    2 days ago










  • That won't result in a NPE.
    – Nicholas K
    2 days ago










  • @NicholasK how to make radioButton not null?
    – Skiwmish
    2 days ago














up vote
0
down vote













Looks like radioButton.getText() returns a String. You can't use == to compare Strings use .equals() instead.



Change it to :



 if ("Circle".equals(radioButton.getText()) {





share|improve this answer




















  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
    – Skiwmish
    2 days ago










  • Looks like radioButton is null.
    – Nicholas K
    2 days ago










  • @NicholasK i think you are missing .toString() after getText()
    – Saurabh Bhandari
    2 days ago










  • That won't result in a NPE.
    – Nicholas K
    2 days ago










  • @NicholasK how to make radioButton not null?
    – Skiwmish
    2 days ago












up vote
0
down vote










up vote
0
down vote









Looks like radioButton.getText() returns a String. You can't use == to compare Strings use .equals() instead.



Change it to :



 if ("Circle".equals(radioButton.getText()) {





share|improve this answer












Looks like radioButton.getText() returns a String. You can't use == to compare Strings use .equals() instead.



Change it to :



 if ("Circle".equals(radioButton.getText()) {






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Nicholas K

3,61431029




3,61431029











  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
    – Skiwmish
    2 days ago










  • Looks like radioButton is null.
    – Nicholas K
    2 days ago










  • @NicholasK i think you are missing .toString() after getText()
    – Saurabh Bhandari
    2 days ago










  • That won't result in a NPE.
    – Nicholas K
    2 days ago










  • @NicholasK how to make radioButton not null?
    – Skiwmish
    2 days ago
















  • java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
    – Skiwmish
    2 days ago










  • Looks like radioButton is null.
    – Nicholas K
    2 days ago










  • @NicholasK i think you are missing .toString() after getText()
    – Saurabh Bhandari
    2 days ago










  • That won't result in a NPE.
    – Nicholas K
    2 days ago










  • @NicholasK how to make radioButton not null?
    – Skiwmish
    2 days ago















java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
– Skiwmish
2 days ago




java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.locator.myapplication.MainActivity$3.onClick(MainActivity.java:87) it says at the logcat sir
– Skiwmish
2 days ago












Looks like radioButton is null.
– Nicholas K
2 days ago




Looks like radioButton is null.
– Nicholas K
2 days ago












@NicholasK i think you are missing .toString() after getText()
– Saurabh Bhandari
2 days ago




@NicholasK i think you are missing .toString() after getText()
– Saurabh Bhandari
2 days ago












That won't result in a NPE.
– Nicholas K
2 days ago




That won't result in a NPE.
– Nicholas K
2 days ago












@NicholasK how to make radioButton not null?
– Skiwmish
2 days ago




@NicholasK how to make radioButton not null?
– Skiwmish
2 days ago



這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands