Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListFragment中的上下文菜单_Android_Android Layout_Android Widget_Android Listview_Android Fragments - Fatal编程技术网

Android ListFragment中的上下文菜单

Android ListFragment中的上下文菜单,android,android-layout,android-widget,android-listview,android-fragments,Android,Android Layout,Android Widget,Android Listview,Android Fragments,我正在尝试为平板电脑创建一个应用程序,该应用程序的主屏幕上有三个片段,每个片段都包含一个列表。我想为每个列表启用上下文菜单,但每当我尝试时,我会得到一个意外的程序停止并强制关闭 <TextView android:id="@+id/txtHeader1" android:layout_width="fill_parent" android:layout_height="30dp" android:layout_grav

我正在尝试为平板电脑创建一个应用程序,该应用程序的主屏幕上有三个片段,每个片段都包含一个列表。我想为每个列表启用上下文菜单,但每当我尝试时,我会得到一个意外的程序停止并强制关闭

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
以下是相关的代码和xml,在我尝试添加上下文菜单之前,它们可以为我提供所需的三个片段,每个片段中都包含ListView:

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp">

<fragment class="cdc.ListFragment.Fragment1"
    android:id="@+id/fragment1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<fragment class="cdc.ListFragment.Fragment2"
    android:id="@+id/fragment2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

<fragment class="cdc.ListFragment.Fragment3" 
    android:id="@+id/fragment3"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" />

</LinearLayout>
    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
Fragment1.java(其他两个类似)

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
导入android.app.ListFragment;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类Fragment1扩展了ListFragment{
字符串[]总统={“德怀特·D·艾森豪威尔”,“约翰·F·肯尼迪”,
“林登·约翰逊”、“理查德·尼克松”、“杰拉尔德·福特”,
“吉米·卡特”、“罗纳德·里根”、“乔治·H·W·布什”,
“比尔·克林顿”、“乔治·W·布什”、“巴拉克·奥巴马”};
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
返回充气机。充气(R.layout.fragment1,容器,假);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setListAdapter(新的ArrayAdapter(getActivity()),
android.R.layout.simple_list_item_1,总统);
}
}
根据我读到的所有内容,我应该能够简单地添加
registerForContextMenu(getListView())
添加到fragment1.java中的onCreate方法,并添加适当的菜单代码。然而,当我添加它并尝试运行它时,我得到了前面提到的锁/崩溃

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
有人对这种情况有什么建议/帮助吗?

Move

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
registerForContextMenu(getListView());

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>
它应该可以解决这个问题

    <TextView
        android:id="@+id/txtHeader1"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/machines_header"
        android:textColor="#00ccff"
        android:background="#ff23cf"
        android:textSize="25dp" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        android:textSize="12dp" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="25dp"
        android:text="@string/menu_add_machine"
        android:textSize="15dp" >
    </Button>

</LinearLayout>