Próbuję utworzyć aplikację dla tabletów z głównym ekranem z trzema fragmentami, z których każdy zawiera listę. Chciałbym włączyć menu kontekstowe dla każdej listy, ale gdy tylko spróbuję, otrzymam niespodziewany zatrzymanie programu i wymuszam zamknięcie.Menu kontekstowe z ListFragment
Poniżej znajduje się odpowiedni kod i xml, który działa i daje mi moje pożądanych trzy fragmenty z listviews w każdym, zanim spróbuję dodać do menu kontekstowego:
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 (dwa inne są podobne)
<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 (dwa pozostałe są podobne)
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));
}
}
Według wszystko czytałem, powinienem móc po prostu dodać
registerForContextMenu(getListView());
metody onCreate w fragment1. java i dodaj odpowiedni kod menu. Jednakże, gdy dodam to i spróbuję go uruchomić, otrzymuję wspomniany wcześniej zamek/awarię.
Ktoś ma jakieś wskazówki/pomoc w tej sytuacji?
Wow ... to proste. :) Dziękuję Ci! – Barak