If else statement in new Intent [duplicate]
up vote
-1
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.
java
marked as duplicate by Andy Turner
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();
);
);
);
Nov 10 at 10:10
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.
add a comment |
up vote
-1
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.
java
marked as duplicate by Andy Turner
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();
);
);
);
Nov 10 at 10:10
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.
Nov 10 at 10:07
1
Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
Nov 10 at 10:15
"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
Nov 10 at 10:33
add a comment |
up vote
-1
down vote
favorite
up vote
-1
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.
java
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
java
asked Nov 10 at 10:05
Skiwmish
115
115
marked as duplicate by Andy Turner
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();
);
);
);
Nov 10 at 10:10
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
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();
);
);
);
Nov 10 at 10:10
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.
Nov 10 at 10:07
1
Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
Nov 10 at 10:15
"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
Nov 10 at 10:33
add a comment |
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.
Nov 10 at 10:07
1
Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
Nov 10 at 10:15
"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
Nov 10 at 10:33
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.
Nov 10 at 10:07
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.
Nov 10 at 10:07
1
1
Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
Nov 10 at 10:15
Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
Nov 10 at 10:15
"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
Nov 10 at 10:33
"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
Nov 10 at 10:33
add a comment |
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()) {
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
Nov 10 at 10:33
Looks likeradioButtonis null.
– Nicholas K
Nov 10 at 10:34
@NicholasK i think you are missing.toString()aftergetText()
– Saurabh Bhandari
Nov 10 at 10:36
That won't result in aNPE.
– Nicholas K
Nov 10 at 10:42
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
|
show 1 more comment
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()) {
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
Nov 10 at 10:33
Looks likeradioButtonis null.
– Nicholas K
Nov 10 at 10:34
@NicholasK i think you are missing.toString()aftergetText()
– Saurabh Bhandari
Nov 10 at 10:36
That won't result in aNPE.
– Nicholas K
Nov 10 at 10:42
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
|
show 1 more comment
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()) {
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
Nov 10 at 10:33
Looks likeradioButtonis null.
– Nicholas K
Nov 10 at 10:34
@NicholasK i think you are missing.toString()aftergetText()
– Saurabh Bhandari
Nov 10 at 10:36
That won't result in aNPE.
– Nicholas K
Nov 10 at 10:42
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
|
show 1 more comment
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()) {
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()) {
answered Nov 10 at 10:06
Nicholas K
4,55741029
4,55741029
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
Nov 10 at 10:33
Looks likeradioButtonis null.
– Nicholas K
Nov 10 at 10:34
@NicholasK i think you are missing.toString()aftergetText()
– Saurabh Bhandari
Nov 10 at 10:36
That won't result in aNPE.
– Nicholas K
Nov 10 at 10:42
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
|
show 1 more comment
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
Nov 10 at 10:33
Looks likeradioButtonis null.
– Nicholas K
Nov 10 at 10:34
@NicholasK i think you are missing.toString()aftergetText()
– Saurabh Bhandari
Nov 10 at 10:36
That won't result in aNPE.
– Nicholas K
Nov 10 at 10:42
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
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
Nov 10 at 10:33
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
Nov 10 at 10:33
Looks like
radioButton is null.– Nicholas K
Nov 10 at 10:34
Looks like
radioButton is null.– Nicholas K
Nov 10 at 10:34
@NicholasK i think you are missing
.toString() after getText()– Saurabh Bhandari
Nov 10 at 10:36
@NicholasK i think you are missing
.toString() after getText()– Saurabh Bhandari
Nov 10 at 10:36
That won't result in a
NPE.– Nicholas K
Nov 10 at 10:42
That won't result in a
NPE.– Nicholas K
Nov 10 at 10:42
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
@NicholasK how to make radioButton not null?
– Skiwmish
Nov 10 at 10:56
|
show 1 more comment
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.
Nov 10 at 10:07
1
Make following corrections(Note the order of the comparison): if ("Circle".equals(radioButton.getText().toString()))...
– HaroldSer
Nov 10 at 10:15
"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
Nov 10 at 10:33