2013-08-31 52 views
0

我有ListView與自定義適配器。直到現在我還有長按上下文菜單。並以此onContextItemSelected - 項目ID空指針異常

getListView().setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
    { 

     public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
     { 
      //To register the button with context menu. 
          registerForContextMenu(getListView()); 
          openContextMenu(getListView()); 

     } 
    }); 

的點擊重新做這個insted的只有這

registerForContextMenu(getListView()); 

這種改變它的工作原理確定之前,但現在我有NullPointerException異常在info.id,我不知道TI如何解決它。感謝您的迴應。

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
    Intent i; 
    Log.d("item id", String.valueOf(item.getItemId())); 
    switch (item.getItemId()) { 

    case R.id.spotreba:  
     i = new Intent(this, SpotrebaActivity.class); 
     i.putExtra("idAuta", info.id); 
     startActivityForResult(i, REQUEST_ADD_AUTO); 
     return true; 
     . 
     . 
     . 

回答

0

你應該在活動中與ListView,而不是在內部類u've發(onItemClickListener)註冊上下文菜單..

把它寫成..

registerForContextMenu(getListView()); 
getListView().setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
    { 

     public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
     { 
       openContextMenu(getListView()); 
     } 
    }); 
+0

我做到了,但同樣的錯誤: - / – Liner