package rms.example.com.rms;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class CaddActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadd);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cadd, 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);
}
}
please help me in my code as i receive error in cadd (cannot resolve symbol cadd) .
my other classes also have the same problem.
this is my uni project and i am new to android programming
thank you for your support
In Android Studio, from the top toolbar select Build => Rebuild Project. When it ends, the error should be fixed.
If it is not, have a look which word in the code is highlighted in red. If it is in this line:
getMenuInflater().inflate(R.menu.cadd, menu);
Make sure you have a resource in the menu directory called 'cadd'. If not and you do not need it, just remove the line.
Check if you have cadd named menu_layout in res/menu folder.
If yes then try clean and Rebuild your project.
From the following example create cadd file for your menu.
As you define here..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cadd, menu);
return true;
}
cadd
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".CaddActivity">
</menu>
is cadd resource file have in your menu directory?i think the file is not there so try to add cadd menu resource file other wile use default menu_main file in place of cadd
it was solved,
it was that i forgot to declare in the menu
Related
I have been practicing in Android Studio as per the android app development free course in UDACITY. And wherever there is a R.menu or R. stuff it cannot be resolved
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.widget.TextView;
import android.R;
public class MainActivity extends AppCompatActivity {
#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.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);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
Plz help how to solve this Issue.
Clean and rebuild your project by going to Build>Clean Project.
Go to File>Invalidate Caches/Restart.
Make sure you built your project, and make sure there aren't any errors (red lines) in the res directory.
remove import android.R; clean and rebuild it.
if it still doesn't work try this
import com.example.android.courtcounter.R;
Check you AndroidManifest.xml, check the package name there.
use that as import .R; in code.
This is the only error I have now in my MainActivity.java.
The line "if (id == R.id.action_settings)" which is created by eclipse gives the error :"action_settings cannot be resolved or is not a field"
I appreciate your help to solve it
package chapter.two.hello_world;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
WorldGen earth = new WorldGen("Earth", 5973, 9.78);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setStartUpWorldValues();
}
protected void setStartUpWorldValues(){
earth.setPlanetColonies(1);
earth.setPlanetMilitary(1);
earth.setColonyImmigration(1000);
earth.setBaseProtection(100);
earth.turnForceFieldOn();
}
protected void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBaseValue = (TextView)findViewById(R.id.dataView7);
planetBaseValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
}
#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) { // the error is here
return true;
}
return super.onOptionsItemSelected(item);
}
}
Most likely your R.menu.main xml file does not contain an item having #+id/action_settings as its id. Check your R.menu.main xml file ans make sure the action_settings id is set.
As say Quanturium this mean you don't have an item with this name. But as you are talking about "action_setting" you're in fact talking about special item which are not commonly in the XML main file.
Note: I'm not a "pro" in Android, so I just hope this will help.
Have a look at the folder on the left of Android Studio. You have one named "res". Inside you have drawable, values etc... Do you have one named "menu"?
If not create one: clic right on "res", select "New / Android ressource directory" and give "menu" as name.
Now we have a folder which will contain the data for our menu, like action_settings etc...
Now, clic right on this new folder and select "New / Menu ressource file".
The name you'll give must be the name of the activity.
Eg you have a Java piece of code named "Creation", the activity (in the layout folder) is named "activity_creation.xml", so in the menu folder you have to give "creation" as name of the file, which will be created as "creation.xml".
Now, in this file you have to write:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="net.XXX.YYY.ZZZZ" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
where XXXX and YYYY are the directory of your app (same as yu write in top of yur Java code), and ZZZZ is the name of the activity (so in my example "creation")
As know you have a "menu", and an action_settings, in your code the following lines will be OK:
#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);
}
Understanding the goal of each folder in Android is a bit hard but necessary as a lot of "smalls problems" came from that.
In my app, I have an icon in the action bar. When I click on it, I get an option called Settings. However, when I click on this option, nothing happens. I have searched, but I can't find out how to utilize this option. I would like for a dialog box to open when Settingsclicked (or if another Activity opened that would be cool too), which I could then add content to. Does anyone know how I would go about this?
The solution Dave S provided works but I want to share another way you can handle this functionality.
In your Activity you can have the following code to interact with your menu:
#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) {
displaySettingsDialog();
}
return super.onOptionsItemSelected(item);
}
The key is to make sure you are checking for the right ID you assigned to your menu item.
If you wanted to launch a new activity (SettingsActivity) then you can replace the displaySettingsDialog() function with the following:
Intent intent = new Intent(this, SettingsActivity.class)
startActivity(intent);
Look for main.xml in your res/menu/ folder. (at least for eclipse ADT) There you can specify an onClick attribute which will call a function in your code. from there you can do whatever you want with it.
EDIT
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="#+id/fb_login"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/facebook_login_label"
android:onClick="showFacebookDialog"/>
</menu>
This would define the item for my MainActivity, inside which you define the onClick like so
public void showFacebookDialog(final MenuItem item)
{
...
}
package com.yogeshbalan.myrootguide.appguide;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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);
}
}
this is my MainActivity and showing error 'Cannot resolve R to it's type '
whenever i add actionbarsherlock library in my project it doesn't build, but build after removing the library.
the error is
Error Paste Bin
This error is usually caused by either of these possibilities:
The most probable: You have a syntax error in any of your layout files (i.e., the files located under the res\layout folder of your project). The bad thing of this is that Eclipse won't warn you and tell you what's the error, so you'll have to go one by one looking for the syntax (probably an unmatched tag, an unmatched attribute, etc.) and fix the issue.
The other possibility is a syntax error within your AndroidManifest.xml file. Same goes here, check it for syntax errors.
One of these two will fix your issue.
I've stumbled upon an issue that i didn't have before,must've deleted something and messed it up but i really can't figure out where..
I'm doing a menu on the action bar but after i add the items on the main.xml file from the menu folder and setting them id's,it can't find the id's in my activities.What's the issue ? I tried cleaning and other stuff but i can't figure this out...
Here's my onCreateOptionsMenu :
#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) {
switch (item.getItemId()) {
case android.R.id.setari:
// app icon in action bar clicked; go home
finish();
break;
case android.R.id.home:
// app icon in action bar clicked; go home
finish();
break;
}
return false;
}
And here's my main.xml inside the menu folder :
<item
android:id="#+id/setari"
android:orderInCategory="2"
android:showAsAction="ifRoom|withText"
android:title="#string/action_settings"/>
<item android:id="#+id/admprod"
android:title="Administrare produse" android:orderInCategory="1"/>
<item android:id="#+id/despre"
android:title="Despre" android:orderInCategory="3"/>
<item android:id="#+id/iesire"
android:title="Iesire" android:orderInCategory="4"/>
Some things might not have a logic,that's because i tried a lot of stuff to fix this..
If the error is showing for every menu id, then probably you have imported the android.R instead of your package.R .
Make sure that you have imported the R class of your project, not the android.R class.