I keep getting the error message :
"cannot find symbol variable action_settings"
How can I fix this?
I was getting a similar error mesaage regarding this part:
.inflate(R.menu.menu_main, menu);
but then I followed the advice from somebody on here about adding a menu file in the res file.
What should I do about this situation?
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
/**
* This activity keeps track of the basketball score for 2 teams.
*/
public class MainActivity extends AppCompatActivity {
// Tracks the score for Team A
int scoreTeamA = 0;
// Tracks the score for Team B
int scoreTeamB = 0;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimpSlifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
}
}
Your menu menu_main shall contain a certain view(id=action_settings).Or you just remove the line
if (id == R.id.action_settings)
Related
I am trying to build Android app everything worked fine but suddenly my Home Activity is throwing me an error not able to find symbol Menu and MenuItem. I am not sure what it went wrong. I rebuild the project and invalidate and cache the project but still its throwing me an error. Any help is highly appreciated.
import android.view.Menu;
import android.view.MenuItem;
Sample Code
import android.view.Menu;
import android.view.MenuItem;
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
You can try this.
Build-> Clean Project
Build-> Rebuild Project
I am just getting started towards android and i am having trouble with my hello world app, The problem is first it said R cannot resolved which i was able to overcome because there was no R.java files which i later copied from the internet
now there are three errors
1. activity_main cannot be resolved or is not a field
2. menu cannot be resolved or is not a field
3. action_settings cannot be resolved or is not a field
I have already tries control+shift+o,
already tried clean and build.
package my.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}strong text
You should not create or modify R.java file manually as it's autogenerated by build system. Try to remove one copied from Internet and just rebuild your project with layout and menu files located in appropriated directories of the res folder
I'm working with java in Android Studio and trying to convert a string from a TextView into a double so it can be used to calculate some figures on a person's income.
The method ParseDouble will not allow me to run it in this way. I'm wondering why it will not compile. It simply says cannot resolve method. According to the documentation, it accepts a string as a parameter.
Here is my code currently:
package ericleeconklin.costoflivingcalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.lang.Object;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.Double;
public class EnterIncome extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_income);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_enter_income, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void getIncome(View income){
View myIncomeString;
myIncomeString = (TextView)findViewById(R.id.enterIncome);
Double myIncome;
myIncome = Double.ParseDouble(myIncomeString);
}
}
myIncomeString is a TextView you have to get first the text ( which is EditText) and then get toString()
Change to be :
public void getIncome(View income){
TextView myIncomeString;
myIncomeString = (TextView)findViewById(R.id.enterIncome);
Double myIncome;
myIncome = Double.parseDouble(myIncomeString.getText().toString());
}
Method is Double.parseDouble(String) not Double.ParseDouble(String).
It's the same code in the tutorial but when I tried the code. It gives me the above error.
package com.example.rishav_pc.fragments;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListner {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// This gets called by the top fragment when the user clicks the button
#Override
public void createMeme(String top, String bottom) {
BottomPictureFragment bottomFragment = (BottomPictureFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomFragment.setMemeText(top, bottom);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
There is one more error
Error:Execution failed for task ':app:compileDebugJava'.
Compilation failed; see the compiler error output for details.
In your BottomPictureFragment change the import of the extended class Fragment to be import android.support.v4.app.Fragment; . It seems that by you it is something else. You should have something like this :
import android.support.v4.app.Fragment;
...
public class BottomPictureFragment extends Fragment { ... }
I am new to android studio and I am reading the tutorials on i-programmer located here (http://www.i-programmer.info/programming/android/5914-android-adventures-activity-and-ui.html?start=2)
The two objects in the environment are a button and a large text widget instanced
in android studio's xml designer.
The problem: A method for setting text referenced in the tutorial is showing this error message when I try to run the code: error: cannot find symbol method setText(String)
And this error shows up in the text editor: Cannot resolve method 'setText(java.lang.String)'
Provided Source Code:
package com.example.helloworld1.helloworld1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onButtonClick(View v){
Button button=(Button) v;
v.setText("I've Been Clicked!"); // This is where the error happens
}
}
I think you meant to use button instead of v.
button.setText("I've Been Clicked!");