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

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

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.

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

listView = (ListView) findViewById(R.id.listView);

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView&lt;?&gt; 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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout=&quot;@layout/include&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;


    &lt;item
        android:id=&quot;@+id/order_takeout&quot;
        android:layout_width=&quot;145dp&quot;
        android:layout_height=&quot;126dp&quot;
        android:onClick=&quot;doTakeOut&quot;
        android:title=&quot;@string/delete&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

    &lt;item
        android:id=&quot;@+id/order_eat_in&quot;
        android:layout_width=&quot;114dp&quot;
        android:layout_height=&quot;110dp&quot;
        android:onClick=&quot;doEatIn&quot;
        android:title=&quot;@string/edit&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/order_takeout&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;


&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 只用于布局资源文件,而不适用于菜单资源文件。

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

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

  &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
   &lt;item android:id=&quot;@+id/mail&quot;
   android:icon=&quot;@drawable/ic_mail&quot;
   android:title=&quot;@string/mail&quot; /&gt;
    &lt;item android:id=&quot;@+id/upload&quot;
    android:icon=&quot;@drawable/ic_upload&quot;
    android:title=&quot;@string/upload&quot;
    android:showAsAction=&quot;ifRoom&quot; /&gt;
    &lt;item android:id=&quot;@+id/share&quot;
    android:icon=&quot;@drawable/ic_share&quot;
    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:

确定