1
我想爲列表創建一個上下文菜單。實現上下文菜單到ListView?
<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:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
content = new ArrayList<HashMap<String,String>>();
ListView list = (ListView)findViewById(R.id.list);
registerForContextMenu(list);
}
當它到達registerForContextMenu(list);
,我得到一個運行時錯誤。
另一件事,當我在XML中使用android:id="@androidid/list"
時,我該如何引用列表? 我試過ListView list = (ListView)findViewById(android.R.id.list);
,但仍然收到錯誤 註冊到上下文菜單。
列表爲空,因爲您沒有設置內容視圖。 – Luksprog
首先設置內容查看然後使用findViewByID –
,但然後我得到一個錯誤,因爲我在xml中使用android:id =「@ + id/list」 。當我試圖使用android:id =「@ android:id/list」和 ListView list =(ListView)findViewById(android.R.id.list); ,我沒有得到錯誤,但仍然沒有上下文菜單(我沒有實現onCreate) – user1624426