Exception: 期望菜单,但得到了 androidx.constraintlayout.widget.ContstraintLayout

huangapple go评论117阅读模式
英文:

Exception: Expecting menu, got androidx.constraintlayout.widget.ContstraintLayout

问题

我希望你感觉良好。

下面是你提出的问题的翻译部分,代码部分已省略:

所以,针对我的问题,我正在像您在底部看到的那样展开一个布局菜单,但现在在展开布局时出现了错误。

这是我布局的完整代码以及onItemLongClick方法的代码。此外,我想在List项上创建一个长按操作,然后让一个弹出菜单显示在屏幕上,其中包含不同的单词,比如编辑、删除等。

我的popup_layout.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:layout="@layout/include"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <item
  9. android:id="@+id/order_takeout"
  10. android:layout_width="145dp"
  11. android:layout_height="126dp"
  12. android:onClick="doTakeOut"
  13. android:title="@string/delete"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <item
  19. android:id="@+id/order_eat_in"
  20. android:layout_width="114dp"
  21. android:layout_height="110dp"
  22. android:onClick="doEatIn"
  23. android:title="@string/edit"
  24. app:layout_constraintBottom_toTopOf="@+id/order_takeout"
  25. app:layout_constraintEnd_toEndOf="parent"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. </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.

Exception: 期望菜单,但得到了 androidx.constraintlayout.widget.ContstraintLayout

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)?

  1. listView = (ListView) findViewById(R.id.listView);
  2. listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  3. @Override
  4. public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {
  5. PopupMenu p = new PopupMenu(ViewListContents.this, v);
  6. MenuInflater inflater = p.getMenuInflater();
  7. inflater.inflate(R.layout.popup_layout, p.getMenu());
  8. p.show();
  9. return true;
  10. }
  11. });

My popup_layout.xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  5. android:layout=&quot;@layout/include&quot;
  6. android:layout_width=&quot;match_parent&quot;
  7. android:layout_height=&quot;match_parent&quot;&gt;
  8. &lt;item
  9. android:id=&quot;@+id/order_takeout&quot;
  10. android:layout_width=&quot;145dp&quot;
  11. android:layout_height=&quot;126dp&quot;
  12. android:onClick=&quot;doTakeOut&quot;
  13. android:title=&quot;@string/delete&quot;
  14. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  15. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  16. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  17. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  18. &lt;item
  19. android:id=&quot;@+id/order_eat_in&quot;
  20. android:layout_width=&quot;114dp&quot;
  21. android:layout_height=&quot;110dp&quot;
  22. android:onClick=&quot;doEatIn&quot;
  23. android:title=&quot;@string/edit&quot;
  24. app:layout_constraintBottom_toTopOf=&quot;@+id/order_takeout&quot;
  25. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  26. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  27. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  28. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

I would like to thank you in advance for any feedback

答案1

得分: 1

你需要一个菜单资源文件,不能使用布局资源文件。将你的 XML 文件位置更改为菜单资源目录。如果你没有菜单资源目录,创建一个并将你的 XML 文件(pop_layout.xml)放在其中。另外,菜单资源文件中不能有约束布局,ViewGroup 只用于布局资源文件,而不适用于菜单资源文件。

菜单资源文件的结构如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:id="@+id/mail"
  4. android:icon="@drawable/ic_mail"
  5. android:title="@string/mail" />
  6. <item android:id="@+id/upload"
  7. android:icon="@drawable/ic_upload"
  8. android:title="@string/upload"
  9. android:showAsAction="ifRoom" />
  10. <item android:id="@+id/share"
  11. android:icon="@drawable/ic_share"
  12. android:title="@string/share" />
  13. </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 :

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  3. &lt;item android:id=&quot;@+id/mail&quot;
  4. android:icon=&quot;@drawable/ic_mail&quot;
  5. android:title=&quot;@string/mail&quot; /&gt;
  6. &lt;item android:id=&quot;@+id/upload&quot;
  7. android:icon=&quot;@drawable/ic_upload&quot;
  8. android:title=&quot;@string/upload&quot;
  9. android:showAsAction=&quot;ifRoom&quot; /&gt;
  10. &lt;item android:id=&quot;@+id/share&quot;
  11. android:icon=&quot;@drawable/ic_share&quot;
  12. android:title=&quot;@string/share&quot; /&gt;

</menu>

Hope that helps.

huangapple
  • 本文由 发表于 2020年10月8日 13:23:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64256276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定