Android Studio cannot resolve symbol 'equals' - android

I was trying to make a quiz app, that would change the question every time you submit, and it should work except for .equals doesn't work, and it cannot resolve the symbol.
its crazy! Any nelp would be apreciated

I guess you have added above code portion outside of a Method(life-cycle callback method or your own method) that's why you are getting error:
cannot resolve symbol 'equals'.
SOLUTION:
Just move your code portion inside a method. Here is an example:
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
..........
.................
int QuizSelection = 20;
Intent gotosetone = getIntent();
Intent gotosetone2 = getIntent();
String randomintent1 = gotosetone.getStringExtra(AlarmClock.EXTRA_MESSAGE);
String randomintent2 = gotosetone2.getStringExtra(AlarmClock.EXTRA_MESSAGE);
if (randomintent1.equals("Quiz One")){
QuizSelection = 1;
} else if (randomintent2.equals("Quiz Two")){
QuizSelection = 2;
}
...........
..................
}
Hope this will help~

Related

Average calculator Fatal Exception

I am really new at programming. I am trying to run a simple average calculator and getting a force close, this is what the logcat is showing. I am running android studio version 2.3.3
FATAL EXCEPTION: main
Process: com.vu.gradingapp, PID: 6312
java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:620)
at java.lang.Integer.valueOf(Integer.java:794)
at com.vu.gradingapp.AverageActivity$1.onClick(AverageActivity.java:37)
at android.view.View.performClick(View.java:6219)
at android.view.View$PerformClick.run(View.java:24482)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
The code i am using is as follow
public class AverageActivity extends AppCompatActivity {
EditText editmanner, editinstances, editshortstance, editstrikes, editboxingskills, editknocks, editkicks, editResults;
Button btnResults;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.average_page);
editmanner =(EditText)findViewById(R.id.editText8);
editinstances = (EditText)findViewById(R.id.editText9);
editshortstance = (EditText)findViewById(R.id.editText10);
editstrikes = (EditText)findViewById(R.id.editText11);
editboxingskills = (EditText)findViewById(R.id.editText12);
editknocks = (EditText)findViewById(R.id.editText13);
editkicks = (EditText)findViewById(R.id.editText14);
editResults = (EditText)findViewById(R.id.editText15);
btnResults = (Button) findViewById(R.id.button10);
btnResults.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int first, second, third, fourth, fifth, sixth, seventh, results;
first=Integer.valueOf(editmanner.getText().toString());
second=Integer.valueOf(editinstances.getText().toString());
third=Integer.valueOf(editshortstance.getText().toString());
fourth=Integer.valueOf(editstrikes.getText().toString());
fifth=Integer.valueOf(editboxingskills.getText().toString());
sixth=Integer.valueOf(editknocks.getText().toString());
seventh=Integer.valueOf(editkicks.getText().toString());
results=(first+second+third+fourth+fifth+sixth+seventh)/7;
editResults.setText(String.valueOf(results));
}
});
}
public void knowtheresults (View view) {
String button_text;
button_text = ((Button) view).getText().toString();
if (button_text.equals("Summary")) {
Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);
} else if (button_text.equals("Back")) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
WHat am I doing wrong :D
Thanks Guys
Error says you cannot convert "" value to number Its a java.lang.NumberFormatException. So use 0 instead of number is blank first check if edittext is blank than set its value to 0
int first;
if(editmanner.getText().toString().equals("")){
first = 0
}else{
first = Integer.valueOf(editmanner.getText().toString());
}
do this for your all int variables.
Update your code with such verification for each int variable:
if (editmanner().toString().isEmpty())
first = 0;
else
first = Integer.parseInt(editmanner().toString());
Since you have int variables (not Integer) it is preferable to use parseInt instead valueOf
Thanks guys, I just filled in the textedits with (0) and now is working. Please apologize for my silly questions.
Is there any way to hide this zeros on the textview so it shows blanks.
java.lang.NumberFormatException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
The logcat already stated what is the cause of the crash. One or all of your EditText has empty string. To convert string to Integer, the string must be numeric(0,1,2 etc). You need to set EditText with numeric value (eg: 0).

i tried to display addition result to others activity but didn't appear

I am newbie at this javascript and android stuff.
I'm tried to add two number taken from "EditView" and display it in other activity.
But it's didn't show up anything.
here are the code.
public void lihathasil(View v) {
thn1 = (EditText) findViewById(R.id.datat1);
thn2 = (EditText) findViewById(R.id.datat2);
thn3 = (EditText) findViewById(R.id.datat3);
thn4 = (EditText) findViewById(R.id.datat4);
thn1cus = Integer.parseInt(thn1.getText().toString());
thn2cus = Integer.parseInt(thn2.getText().toString());
thn3cus = Integer.parseInt(thn3.getText().toString());
thn4cus = Integer.parseInt(thn4.getText().toString());
ab = thn1cus + thn2cus + thn3cus + thn4cus;
Intent i = new Intent(getApplicationContext(), GeneralReport.class);
i.putExtra("pertama",ab);
startActivity(i);
}
And here are the code in other activity to view the result
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general_report);
tam1=(TextView) findViewById(R.id.tampilsatu);
val1=getIntent().getExtras().getString("pertama");
tam1.setText(val1);
}
when i run it's display nothing.
can anyone help to solved it.
thanks
You pass the integer value in one activity and get the string value in other activity.
Other activty code :
val1=getIntent().getExtras().getIntExtra("pertama",0);
tam1.setText(String.valueof(val1));
In your second Activity change this
val1=getIntent().getExtras().getString("pertama");
to this
val1=getIntent().getIntExtra("pertama", 0);
and change this
tam1.setText(val1);
to this
tam1.setText(String.valueOf(val1));
// Assuming val1 is defined as integer

How can dynamically setContentView Android

I've to dynamically set an xml into a ContentView?
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
value = extras.getString("ID_position");
Log.e("TESTVALUE", value);
}
if (value.equals("0")){
setContentView(R.layout.list_mobile);
}
if (value.equals("1")){
setContentView(R.layout.custom_dialog1);
}
}
It crash at activity open... pls help me ^^
PS: the value is correct ;)
EDIT: i've done a big mistake in extend class... damned copy and paste... i'm sorry again ;) now it work well
Looking at your code, the possible reason is:
Value at the Bundle with id = ID_position has type other than String => value will be null and you'll get NPE at value.equals(). Are you sure you didn't put int instead?

NullPointerException when i tried to get Extras in FragmentActivity

i have a problem to pass extras between an Activity and a FragmentActivity.
This next code create the Intent to start the new Activity:
Intent fichaSerie = new Intent(getActivity(),ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getID());
getActivity().startActivity(fichaSerie);
When I tried to get the Extras in the Fragment Activity, i get a NullPointerException:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
String extra = getIntent().getExtras().getString("serie") //NullPointerException
}
Anyone know what mistake(s) i'm made in my code?
Thank you very much to all.
Thanks all your help guys, i solved my problem using this call:
getIntent().getStringExtra("serie")
I mark this question as solved.
Thank you again guys.
First of all Java is case sensitive so .getID() is not the same as .getId().
If you are trying to get an ID of an objet, use .getId(). That usually returns an Integer value
Pay attention on the type you are putting to extra as Michal said (String or Integer or anything else)
Intent fichaSerie = new Intent(getApplicationContext(), ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getId()); //presuming this id is an Integer or long int ... You could change this to string if it is still usable then (serie.getId().toString())... but you have to get an intent with myIntent.getStringExtra(...), but I think you will not be doing this.
startActivityForResult(fichaSerie, 0);
Getting an extra from ActividadSerie.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
Intent myIntent = getIntent();
String extra = myIntent.getStringExtra("serie"); //try diferent gets... getIntExtra(), getStringExtra()..
}
You are put an Integer value and get it into the String value so that it gets a NullPointerException.

why am i getting this error?? Syntax error on token "void", # expected android programming

please help me with the following code...
i am trying to make an android application and i get an error in the code when i try to create a function inside onCreate..
the need to create a function is to access buttons, labels and textboxes....
here goes my code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText player = (EditText)findViewById(R.id.player);
final TextView plrlbl1 = (TextView)findViewById(R.id.textView1);
final ImageButton imageButton1 = (ImageButton)findViewById(R.id.imageButton1);
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
public void thisfunction()
{
}
}
please help me create a function inside of oncreate...
help appreciated
public void thisfunction()
{
}
you declare the thisfunction() inside the onCreate(). You should declare outside the onCreate. If you are having this kind of issue I recommend you to read a basic programming book.
please help me create a function inside of oncreate...
You can't, Java doesn't allow it. You have to declare it outside of onCreate().
The correct setup is something like this
public class MyActivity extended Activity {
private TextView myTextView;
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate () ;
setContentView(R.layout.main) ;
myTextView = (TextView) findViewById (R.id.textView1) ;
myNewMethod() ;
}
private void myNewMethod () {
myTextView.setText("Hello world") ;
}
}

Categories

Resources