8

メイン画面に3つのフラグメントがあり、それぞれがリストを含むタブレット用のアプリケーションを作成しようとしています。私は各リストのコンテキストメニューを有効にしたいと思いますが、私がしようとするたびに、予期しないプログラムの停止と強制終了が発生します。ListFragmentのコンテキストメニュー

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:

後に動作し、私がしようとすると、コンテキストメニューを追加する前に、各リストビューを私に私の希望の3つの断片を与え、関連するコードとxmlです。 XML(他の2つは類似している)

<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(他の2つは似ています)

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)); 
    } 

} 

私が読んだすべてのものによると、私は単にfragment1内のonCreateメソッドに
registerForContextMenu(getListView());
を追加することができるはずです。適切なメニューコードを追加してください。しかし、2番目にそれを追加して実行しようとすると、私は以前に言及したロック/クラッシュを取得します。

このような状況では、誰かが助言をお持ちですか?

public void onActivityCreated(Bundle savedState) { 

答えて

34

移動

registerForContextMenu(getListView()); 

、それは問題を修正する必要があります。

+0

うわー...そんなに簡単。 :) ありがとうございました! – Barak

関連する問題