Action Settings


#1

So while the textbook told me that action settings declaration was supposed to already be in the strings.xml it was not so I wrote it in.
Settings
But when I wrote onOptionsItemSelected method its giving me error messages when using action_settings (this is also happening for menu item in the onCreateOptionsMenu method).

[code]package com.bignerdranch.android.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class QuizActivity extends AppCompatActivity {

private Button mTrueButton;
private Button mFalseButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.quiz, menu); //error
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();
    if(id == R.id.action_settings){ //error
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}[/code]

[code]<?xml version="1.0" encoding="utf-8"?>

GeoQuiz

<string name="question_text">
    Constantinople is the largest city in Turkey.
</string>
<string name="true_button">
    True
</string>
<string name="false_button">
    False
</string>

<string name="action_settings">Settings</string>
[/code]

[code]<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/question_text"    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"    >

    <Button
        android:id="@+id/true_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/true_button"  />

    <Button
        android:id="@+id/false_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/false_button" />

</LinearLayout>
[/code]

#2

I am having the same problem.


#3

If you’re having this issue, you are likely on a pretty old version of Android Studio. Google has changed the new project template a few times and this is one of the changes.

R.id.action_settings is an ID attribute and not a string attribute. You can tell because of the id in the name. So, that means it’s defined in an xml file like we’ve done in the book for our IDs. This is actually part of the menu system for Android and you would find the menu files in res/menu/.... It’s not important at this time though. We talk about the menu system later on in the book.

To fix this, just remove the onCreateOptionsMenu and onOptionsItemSelected methods from your activity. Delete those two methods (they are used for the menu system).


#4

how to fix the action_setting error
Unfortunately I delete the action_setting string in the string file. How to get rid of this