从ListFragment上下文菜单 [英] Context menu from a ListFragment
问题描述
我想创建为平板电脑的应用程序,有三个片段,每个包含列表的主屏幕。我想,以使每个列表上下文菜单,但每当我尝试,我得到一个意外的程序停止,强制关闭。
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.
以下是相关的code和XML的作品,给我我想要的三个片段,在每个列表视图,之前,我尝试添加上下文菜单:
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(另外两个是类似的)
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
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(另外两个是类似的)
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));
}
}
据我读过的一切,我应该能够简单地添加
registerForContextMenu(getListView());
在fragment1.java的onCreate方法,并添加相应的菜单code。但是,第二个我再补充一点,并尝试运行它,我得到了previously提到的锁定/崩溃。
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?
推荐答案
移动
registerForContextMenu(getListView());
到
public void onActivityCreated(Bundle savedState) {
和它应该解决这个问题。
and it should fix the problem.
这篇关于从ListFragment上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!