英文:
Exception: Expecting menu, got androidx.constraintlayout.widget.ContstraintLayout
问题
我希望你感觉良好。
下面是你提出的问题的翻译部分,代码部分已省略:
所以,针对我的问题,我正在像您在底部看到的那样展开一个布局菜单,但现在在展开布局时出现了错误。
这是我布局的完整代码以及onItemLongClick方法的代码。此外,我想在List项上创建一个长按操作,然后让一个弹出菜单显示在屏幕上,其中包含不同的单词,比如编辑、删除等。
我的popup_layout.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" android:layout="@layout/include" android:layout_width="match_parent" android:layout_height="match_parent"> <item android:id="@+id/order_takeout" android:layout_width="145dp" android:layout_height="126dp" android:onClick="doTakeOut" android:title="@string/delete" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <item android:id="@+id/order_eat_in" android:layout_width="114dp" android:layout_height="110dp" android:onClick="doEatIn" android:title="@string/edit" app:layout_constraintBottom_toTopOf="@+id/order_takeout" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
非常感谢您提前的任何反馈。
如果您还有其他问题,欢迎随时提问。
英文:
I hope you feel good.
So to my question, i am inflating a layout menu like you see at the bottom, but now I'll get an error while inflating the layout.
Here's the complete code of my layout and of the on ItemLongClick method. In addition I wanna create a long click on a Listitem and then let a popup came on the screen with different words like edit delete whatever.
Is there anything wrong with my layout (ConstraintLayout)?
listView = (ListView) findViewById(R.id.listView);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
PopupMenu p = new PopupMenu(ViewListContents.this, v);
MenuInflater inflater = p.getMenuInflater();
inflater.inflate(R.layout.popup_layout, p.getMenu());
p.show();
return true;
}
});
My popup_layout.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"
android:layout="@layout/include"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item
android:id="@+id/order_takeout"
android:layout_width="145dp"
android:layout_height="126dp"
android:onClick="doTakeOut"
android:title="@string/delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<item
android:id="@+id/order_eat_in"
android:layout_width="114dp"
android:layout_height="110dp"
android:onClick="doEatIn"
android:title="@string/edit"
app:layout_constraintBottom_toTopOf="@+id/order_takeout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I would like to thank you in advance for any feedback
答案1
得分: 1
你需要一个菜单资源文件,不能使用布局资源文件。将你的 XML 文件位置更改为菜单资源目录。如果你没有菜单资源目录,创建一个并将你的 XML 文件(pop_layout.xml)放在其中。另外,菜单资源文件中不能有约束布局,ViewGroup 只用于布局资源文件,而不适用于菜单资源文件。
菜单资源文件的结构如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
希望对你有所帮助。
英文:
You need a menu resource file, you can't have a layout resource file. Change the location of your XML file to the menu resource directory. If You don't have a menu resource directory, create it and place your XML file(pop_layout.xml) there.Also, you can't have a constraint layout in a menu resource file.ViewGroups are only for layout resource files,not for menu resource files.
Menu resporce files look like this :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
Hope that helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论