我正在尝试实现搜索视图,按照我已将options_menu.xml添加到我的项目中的文档进行操作:
<?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>
我尝试在我的MainActivity.kt中膨胀菜单:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.options_menu, menu)
return true
}
但是当我运行该程序时出现以下错误:
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)
我activity_main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FFF"
android:elevation="0dp"
app:titleTextColor="@color/colorPrimary"
android:theme="@style/ThemeOverlay.MaterialComponents.Toolbar.Surface"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/ThemeOverlay.MaterialComponents.Toolbar.Primary" />
<Button
android:id="@+id/showNotifBtn"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginEnd="36dp"
android:text="Show Notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<Button
android:id="@+id/showLocationBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Location"
app:layout_constraintEnd_toStartOf="@+id/showNotifBtn"
app:layout_constraintHorizontal_bias="0.614"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<TextView
android:id="@+id/locationText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/showLocationBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>```
我只是按照文档不知道出了什么问题,错误代码没有指定我缺少的资源。帮助。
你的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>