2017-05-23 7 views
0

ListViewでアイテムリストに触れたときに、コンテキストメニューが表示されていました。しかし、私は1つを取得しませんでした。ここ は、コードは、Javaのためである:ここではAndroid:コンテキストメニューのコンパイルエラー

public class MainActivity extends Activity { 
     ListView listView; 
     List<String> list=new ArrayList<>(); 
     ArrayAdapter<String> adapter; 
     @Override 
      protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      listView= (ListView) findViewById(R.id.list_view); 
      list.add("Strawberry"); 
      list.add("Gooseberry"); 
      list.add("Blueberry"); 
      list.add("Mulberry"); 
      list.add("Raspberry"); 
      adapter=new ArrayAdapter<> 
       (this,android.R.layout.simple_expandable_list_item_1,list); 
      listView.setAdapter(adapter); 
      registerForContextMenu(listView);//pass listView to 
       registerForContextMenu 

     } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
            ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.context_menu, menu); 
    } 
} 

はactivity_mainのためのXMLコードである:ここで

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ccc" 
    android:orientation="vertical" 
    tools:context="com.iox_prime.menudemo.MainActivity"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="MENU DEMO" 
      android:textColor="#000" 
      android:textSize="22sp" 
      android:textStyle="bold" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="#999" /> 

    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="5dp" 
     android:layout_marginStart="5dp"/> 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="#999" /> 
</LinearLayout> 

はメニューのためのXMLです:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/delete" 
     android:title="Delete"/> 
    <item android:id="@+id/edit" 
     android:title="Edit"/> 
</menu> 

何の実行時にエラーがありませんでした。しかし、ブレークポイントを持つデバッグモードでプログラムを実行すると、ClassNotFoundExceptionがスローされます。それはandroid.widget.ViewStubを見つけることができないと言います。付属の画像をご確認ください。 enter image description here

+0

可能回答:https://stackoverflow.com/questions/31678325/android-studio-1-2-2-classnotfoundexception-android-widget-viewstub – buzzingsilently

答えて

0

見つかった回答:実際、これはとても簡単でした。コンテキストメニューは決して触れることを意味しませんでした。触れて保持することを意図していました。私はメニューアイテムに触れ、それを保持し、私が望むように機能しました!

関連する問題