膨胀菜单上的ResourceNotFoundException

时间:2019-12-13 18:26:51

标签: android kotlin searchview

根据文档,我正在尝试将search_menu.xml添加到我的项目中:

   [Route("api/[controller]")]
    [ApiController]
    [Authorize(AuthenticationSchemes = "AAD,CUSTOM")] //<-- yours schema
    public class AccountController : Controller
    {
 // ...
}

然后我尝试在MainActivity.kt中添加菜单:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <menu xmlns:android="http://schemas.android.com/apk/res/android" name="options_menu">
        <item
            android:id="@+id/search"
            android:title="@string/search_title"
            android:icon="@drawable/ic_search"
            android:showAsAction="collapseActionView|ifRoom"
            android:actionViewClass="android.widget.SearchView" />
    </menu>
</resources>

但是运行程序时出现以下错误:

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.options_menu, menu)
    return true
}

我的activity_main.xml如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kotlinexp, PID: 27607
android.content.res.Resources$NotFoundException: File    from xml type layout resource ID #0x7f0c0000
    at android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:990)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2096)
    at android.content.res.Resources.getLayout(Resources.java:1111)
    at android.view.MenuInflater.inflate(MenuInflater.java:108)
    at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:120)
    at com.kotlinexp.MainActivity.onCreateOptionsMenu(MainActivity.kt:64)
    at android.app.Activity.onCreatePanelMenu(Activity.java:3142)
    at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:325)
    at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2830)
    at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
    at androidx.appcompat.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:455)
    at androidx.appcompat.app.ToolbarActionBar$1.run(ToolbarActionBar.java:56)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.io.FileNotFoundException:   
    at android.content.res.AssetManager.openXmlAssetNative(Native Method)
    at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:519)
    at android.content.res.ResourcesImpl.loadXmlResourceParser(ResourcesImpl.java:975)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2096) 
    at android.content.res.Resources.getLayout(Resources.java:1111) 
    at android.view.MenuInflater.inflate(MenuInflater.java:108) 
    at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:120) 
    at com.kotlinexp.MainActivity.onCreateOptionsMenu(MainActivity.kt:64) 
    at android.app.Activity.onCreatePanelMenu(Activity.java:3142) 
    at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:325) 
    at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94) 
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2830) 
    at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94) 
    at androidx.appcompat.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:455) 
    at androidx.appcompat.app.ToolbarActionBar$1.run(ToolbarActionBar.java:56) 
    at android.os.Handler.handleCallback(Handler.java:751) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

我只是遵循文档不知道出了什么问题,错误代码没有指定我缺少的资源。帮助。

1 个答案:

答案 0 :(得分:0)

您的options_menu应该在res -> menu下创建,并像这样

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
        android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>
相关问题