Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 593
Barak Avatar asked Jan 07 '12 15:01

Barak


1 Answers

Move

registerForContextMenu(getListView());

to

public void onActivityCreated(Bundle savedState) {

and it should fix the problem.

like image 110
jsmith Avatar answered Oct 02 '22 23:10

jsmith