parish
parish

Reputation: 856

Android - the following class could not be found

I am creating bottom menu in my android app

I have created on directory in res/menu/option_menu.xml like this

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <item android:id="@+id/post_offer"
        android:title="postoffer"
        android:icon="@drawable/ic_launcher"
        tools:ignore="HardcodedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <item android:id="@+id/history"
        android:title="history"
        android:icon="@drawable/ic_launcher"
        tools:ignore="HardcodedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <item android:id="@+id/inbox"
        android:title="inbox"
        android:icon="@drawable/ic_launcher"
        tools:ignore="HardcodedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <item android:id="@+id/notifications"
        android:title="notifications"
        android:icon="@drawable/ic_launcher"
        tools:ignore="HardcodedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <item android:id="@+id/people"
        android:title="people"
        android:icon="@drawable/ic_launcher"
        tools:ignore="HardcodedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</menu>

then I included this xml in activity_main.xml and java

<include
        layout="@menu/option_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

and this java

@Override
    public boolean onCreateOptionsMenu(Menu menu){
        //MenuInflater inflater = getMenuInflater();
        //inflater.inflate(R.menu.option_menu, menu);
        //return true;
        getMenuInflater().inflate(R.menu.option_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case R.id.post_offer:
                startActivity(new Intent(this, RegisterActivity.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }

Now problem is when I try to check my activity_main.xml in design mode it showing this following error

Missing Classes

The following classes could not be found:

-Item (Fix build path, Edit XML)

-Menu(Fix build path, Edit XML)

EDIT

I am using

Compile SDK version: API 25:Android 7.1.1(Nougat)

Build Tool version: 25.0.1

Upvotes: 0

Views: 272

Answers (2)

Omar El Halabi
Omar El Halabi

Reputation: 2128

the problem is that the <include> tags works only for layout XML (ones that extend View but a menu certainly does not do that)

Upvotes: 0

azizbekian
azizbekian

Reputation: 62209

then I included this xml in activity_main.xml and java

You shouldn't do that. Just leave the menu xml layout as is and inflate it as you did in onCreateOptionsMenu(). It should work.

Upvotes: 2

Related Questions