无法解析符号'menu'

时间:2016-08-30 09:43:11

标签: java android xml

MainActivity.java

  

此activity_main.java

主javaActity文件

导入的包和库

//Package for project 
package com.example.legend.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

//Main activity main class extending Activity class

public class MainActivity extends AppCompatActivity {

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

    //Menu class

    @Override
    public boolean onCreateOptionMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);//Can't resolve  menu symbol
        return true;
    }
}
  

Manifest.xml文件

AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.legend.myapplication">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
  

String.xml文件

     

应用

的字符串内容文件
<resources>
    <string name="app_name">XXXXXXXXXX</string>
</resources>
  

activity_main.xml中

     

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#da006699"
tools:context="com.example.legend.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="XXXXXXXXX"
        android:textColor="#ffffff"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

因为,您的@Override也显示错误,这意味着在超类上找不到该方法。这是真的,因为AppCompatActivity没有:

public boolean onCreateOptionMenu(Menu menu)

但是,确实有:

public boolean onCreateOptionsMenu(Menu menu){...} // Option -> Options

所以,你需要做出以上改变。除此之外,请确保menu_main.xml目录中有res/menu