cannot resolve symbol 'menu'? - android

I have tried to clean up, rebuild and debug my code. I am making a simple quiz (GEO Quiz). I have a menu folder in my resources but there is nothing in it, but I still can get the error to go away. This is my first app.
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class QuizActivity extends Activity {
private Button tButton;
private Button fButton;
//private Question[] questions= new Question[5];
//private int qNum =0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My layout and string.xml are correct thus far. Whenever i type in R. into the onCreateOptionsMenu class. There is no list or prompt that says menu on it.

You should have menu.xml in menu folder
if not, add the menu.xml file inside /res/menu/ folder, this is an example:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:orderInCategory="75"
android:title="my menu Item!"
app:showAsAction="never"/></menu>

Your menu resource folder does not have any xml file. the menu file should be on the menu resource folder.

The only solution that worked for my mac was to install MAMP. XAMPP did not work and locating files was confusing.
When entering commands in terminal all files had protection and contraints I could not do anything.
So MAMP is the best bet to solve this question. Make sure to use the php file in CLI interpeter.

Related

Can not resolve R.id.toolbar

I'm dipping my toes into Android Development by following along examples from this book. I am unable to get the example below to work, though. Instructions are: 1) New project named Dialog 2) Empty Activity 3) Paste/edit to look like the code below.
The message is that Studio can't resolve: R.id.toolbar, R.id.fab, R.menu, and R.id.action_settings.
I'm running Android Studio 3.1.3 on macOS High Sierra. My best guess is that that either the instructions are missing steps or since the book is ~2 years old Android Studio has changed behavior causing this example to break. I don't know enough about this development process to even know how to start to diagnose this.
In AndroidManifest.xml add this line to the activity block:
android:theme="#style/Theme.AppCompat.Dialog"
And this is the only code file to change (DialogActivity.java) for the project:
package com.example.sample.dialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class DialogActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with an action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_dialog, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_dialog.xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DialogActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The reason you are getting those errors is because Java is looking for references in XML that have not been created. For example, it is looking for a reference called "R.id.fab" which was never created.
To fix this, you are going to have to go into the res folder and create the necessary files. Inside of the res -> layout -> "activity_dialog.xml" file, you will have to create a FAB in order to get rid of that error. You can copy/paste this code.
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>
Here, I create the necessary View in XML, and give it an id called fab so you can reference it in the java code. You will also need to create a menu folder and file, so to do that right click on the res folder, and go to "new Android Resource File". Set the file name to "menu" and the resource type should also be menu. Then when you hit "OK", you will see a new folder called menu, and inside of that a file called "menu.xml".
Inside that "menu.xml" file, you're going to have to create your menu options with an id of "action_settings". You can do that by using the code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_settings" android:title="Settings"/>
</menu>
Lastly, you can create your toolbar by right clicking on the layout folder and selecting new layout resource file. You can name it 'toolbar', and set the root element to android.support.v7.widget.Toolbar. This will generate the appropriate code for you, and you can edit it however you'd like. After that go back into the "activity_dialog.xml" file and use this code:
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
This should get rid of all 4 errors
Double check the id's in the R.layout.activity_dialog file. Android studio will output that message when the id that you are looking for is not found in the inflated layout.
EDIT:
You do not have a Toolbar declared in your XML file. When you want to search for a layout element to use in a Fragment or Activity, you use the id parameter you set in the XML file. If you forget to set the id or use the wrong id, it will tell you that the symbol cannot be resolved. There are too many items to add to your code, but follow the links below and you'll pick it up quickly enough. Let me know if you need more information. Also, CodePath is an excellent resource that I heavily relied on when I started learning Android development.
Look at this for a tutorial for adding a toolbar to a layout file and this for more miscellaneous information.
You have not gotten a reference to the view from the xml.
Get the reference from the xml for example if have a button defined in xml with an id of myBtn i would get the reference as Button button = findViewById(R.id.myBtn).
On the main menu, choose File. Invalidate Caches/Restart. The Invalidate Caches message appears informing you that the caches will be invalidated and rebuilt on the next start. Use buttons in the dialog to invalidate caches.

program crashing once colors.xml added to the values folder android ant compile

I'm very confused as to why my program keeps crashing when I add a colors.xml file to the values folder. It runs absolutely perfectly until I add this file and then the whole program wont load. I checked logcat *:E --->
E/AndroidRuntime( 6041): java.lang.RuntimeException: Unable to start activity ComponentInfo{self.redway.dynamicfragment/self.redway.dynamicfragment.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f050000 for fragment FragmentOne{41e3d1b8 #0 id=0x7f050000}
When I checked in gen to see what 0x7f050000 it turned out to be the reference to a string held in strings.xml so it kind of looks like because I have added the colors.xml file ant is no longer parsing strings.xml.
But I'm not sure I'm very new to android and even newer to using ant (I didn't have this problem with eclipse).
Again I would stress I have made no change to my program or any of the code in it since it was running perfectly simple added this file in /res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue">#00F</color>
<color name="yellow">#FF0</color>
</resources>
UPDATE
Here is the java code for FragmentOne if that helps:
package self.redway.dynamicfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class FragmentOne extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle, savedInstanceState)
{
View view = inflater.inflate(R.layout.frag_one, container, false);
return view;
}
}

choosing resource type during xml file creating in Eclipse

When I want to create a XML file in Android SDK installed ECLIPSE for the purpose of SharedPreferences Activity layout... I do not see that dialog box to appear where I can choose from.
Following are my files.
MainActivity.java
package com.preferences_activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
Preferences.java
package com.preferences_activity;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferences extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:text="#string/button1" />
</RelativeLayout>
preferences_activity.xml
<?xml version="1.0" encoding="UTF-8"?>
In the preferences_activity.xml, this is what I get when I create a xml file.
But I wanted to choose a type called preferences while creating a xml file. preferences layout is one of the type of resource available in the dialog box. Others are like: Layout, Values, Menu, AppWidget Provider Searchable, Animation.
If I had a option to choose a preferences layout option while creating a xml file I would be getting the following code:
<?xml version="1.0" encoding="UTF-8"?>
<PreferencesScreen
xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>
My Question is again: Where can I get that dialog box from while creating xml file where I can choose the resource type?
I am sorry if this question sounds funny. But I am struggling with it for few days in a row.
Thank you all.
For future Visitors:
I was struggling because I was using eclipse app instead of the one that was under adt-bundle. If you use the app under the adt-bundle, that has a logo with {} and light green background than you will have an options to choose a layout or any sorts of resources while creating an xml file. Pls, one more thing to note, I was having hard time to find a wizard for creating a xml file ( I could do it manually with right clicking the project and other and choosing xml file).. but I m talking about the wizard on the menu bar, you should be able to see it if you use this app under adt-bundle instead of regular eclipse. My answer may not be that likable, but sharing for future android beginners.

How can I solve R.layout.main cannot be resolved in my android apps?

Im new as a developer of Android apps. I find a problem. that is R.layout.main cannot be resolved. How can I solve my problem. my code is here. please solve my problem.
package com.android;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
Button bt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.ButtonOk);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "Welcome Android World", 3000).show();
Intent intent = new Intent(HelloActivity.this, DisplayActivity.class);
startActivity(intent);
}
});
}
}
*
and my xml code is here:
*
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ok" android:id="#+id/ButtonOk" android:height="50dp" android:width="100dp"></Button>
</LinearLayout>
Step 1 : Remove import android.R;
Step 2 : Clean And Rebuild (It should work)
If not
Then close the project exits eclipse and open again. Follow the steps it should work.
If not
Change your package
From
import android.R;
To
import yourpackage.R;
It Should work
Remove the line import android.R from the header.
and do a Ctrl + Shift + O
If android.R appears again, then manually write
import <yourpackagename>.R
Looks like you used the package name as com.android.R. Ideally speaking, avoid using package names like com.android. Try to maintain it like
com.companyname.appname
So you get into that habit, and don't have the pain of changing the package name in all the folders once you are about to publish.
Import com.android.R not android.R because your package name is com.android
Try to Clean and Rebuild your application and this should solve the issue.
my friend,
check that whether name of your xml file is "main" because we always write setContentView(R.layout.your_xml_file_name);
If you find my solution worthy then please like it
I also encountered this problem on IntelliJ IDEA and this is how I was able to solve the problem, but first I noticed the following:
this problem did not hinder the execution of the application
the resource file (R.java) located in the out/production/my/package/name directory is automatically updated, but the resource file (R.java) located in the gen/my.package.name directory is not updated automatically
IntelliJ IDEA uses the non-updated R.java for intellisence
So, I copied the layout class that was generated in R.java file in out/production/my/package/name directory, as below
public static final class layout {
public static final int main = 2130903040;
public layout() {
}
}
and I pasted this into the R.java file in gen/my.package.name directory
NOTE:
Please, and please, don't modify the auto-generated resource file R.java file in out/production/my/package/name

findViewByID(R.id.Button1) method not recognizing "id" in argument

For some reason, the "id" in the argument is underlined in red and is "not resolved and or not a field"
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.Button;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button toggleButton = (Button)findViewById(R.id.toggleButton);
toggleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
}
}
Check your gen[Generated Java Files] folder in package explorer.
It may be empty, try
Project --> Clean
This will Generate files
Hope This Helps
It is one of the problems everyone faces with EclipseIDE.
Clean your project and build it once again.
P.S. : Also make sure that the id of the Button in your mail.xml is android:id="#+id/toggleButton"
It is a problem faced in eclipse IDE. I had this problem
When there was an error in one of my xml files
When the package definition in my manifest file has changed
When my generated file is in another package.
Check if any of the above is your problem. May be it will help
Cleaning and building the project might also help you
Just Click on Build Button (Green Hammer).
//Done

Categories

Resources