Search code examples
javaandroidandroid-fragmentsandroid-fragmentactivity

Why isnt my "about" tag recognised in R


I am trying to implement a dialog for an app I am creating for my own educational purposes using this tutorial, http://javatechig.com/android/android-dialog-fragment-example.

In my notepadlistactivity.java I have the section,

 @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
        case R.id.addNote:
            openNote(new Note(noteManager));
            break;

            case android.R.id.home:
            break;

            case android.R.id.about:
                FragmentManager fm = getFragmentManager();
                MyDialogFragment dialogFragment = new MyDialogFragment ();
                dialogFragment.show(fm, "Sample Fragment");
            default:
            return false;
        }
        return true;
    }

For all the other R.id....'s it recognises the word after the dot, but for R.id.about, the about is not recognised. I have checked in the R file and about is defined so I cannot understand what is going wrong.

For your information this is my MyDialogFragment.java,

package com.nicae.Notepad.note_list;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.nicae.Notepad.R;

public class MyDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_sample_dialog, container, false);
        getDialog().setTitle("Simple Dialog");
        return rootView;

        Button dismiss = (Button) rootView.findViewById(R.id.dismiss);
        dismiss.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

This is my fragment_sample_dialog.xml,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="vertical"
    android:id="@+id/about">

   <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet..."
        android:textSize="20dp" />

    <Button
        android:id="@+id/dismiss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dismiss" />
</LinearLayout>

Edit corresponding to the comments below:

Here is my activity_notepad_list.xml,

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/addNote"
        android:title="@string/newNote"
        android:orderInCategory="1"
        app:showAsAction="always"
        android:icon="@drawable/content_new"/>
    <item
        android:id="@+id/about"
        android:title="About"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_ab_other"
        android:orderInCategory="2"/>
</menu>

Solution

  • Note that android.R.id is different than R.id depending on the current package name where the current class is and/or import statements. android.R.id refers to standard values from the Android API. You probably mean to use R.id instead.