ListFragment 的上下文菜单

发布于 2024-12-26 00:09:44 字数 3685 浏览 2 评论 0原文

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

以下是相关的代码和 xml,在我尝试添加上下文菜单之前,它为我提供了我想要的三个片段,每个片段中都有列表视图:

main.xmlfragment1.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>

ListFragment.java

import android.app.Activity;
import android.os.Bundle;

public class ListFragmentExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Fragment1.java(其他两个类似)

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment1 extends ListFragment {
    String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy",
            "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford",
            "Jimmy Carter", "Ronald Reagan", "George H. W. Bush",
            "Bill Clinton", "George W. Bush", "Barack Obama" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, presidents));
    }

}

根据我读过的所有内容,我应该能够简单地添加
registerForContextMenu(getListView());
到fragment1.java中的onCreate方法并添加适当的菜单代码。然而,当我添加它并尝试运行它时,我遇到了前面提到的锁定/崩溃。

有人对这种情况有任何指示/帮助吗?

I am trying to create an application for tablets that has a main screen with three fragments, each containing a list. I would like to enable context menus for each list, but whenever I attempt that I get an unexpected program stop and Force Close.

Following is the relevant code and xml that works and gives me my desired three fragments with listviews in each, before I try and add the context menu:

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>

fragment1.xml (the other two are similar)

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

ListFragment.java

import android.app.Activity;
import android.os.Bundle;

public class ListFragmentExampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Fragment1.java (the other two are similar)

import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment1 extends ListFragment {
    String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy",
            "Lyndon B. Johnson", "Richard Nixon", "Gerald Ford",
            "Jimmy Carter", "Ronald Reagan", "George H. W. Bush",
            "Bill Clinton", "George W. Bush", "Barack Obama" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, presidents));
    }

}

According to everything I've read, I should be able to simply add
registerForContextMenu(getListView());
to the onCreate method in fragment1.java and add the appropriate menu code. However, the second I add that and try to run it, I get the previously mentioned lock/crash.

Anyone have any pointers/help for this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-01-02 00:09:44

移至

registerForContextMenu(getListView());

public void onActivityCreated(Bundle savedState) {

它应该可以解决问题。

Move

registerForContextMenu(getListView());

to

public void onActivityCreated(Bundle savedState) {

and it should fix the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文