2017-05-29 42 views
1

這是我的功能,我應該打個電話應該將哪個視圖對象傳遞給具有View對象作爲菜單參數的函數?

public void myLocation (View v){ 


     MyMapSettings(MY_LOCATION); 
     showMyAddressOnMap(); 
     drawGeofencesAround(MY_LOCATION, false); 


    } 

我打電話從這裏上面的功能:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { 
    case R.id.add: 

     myLocation(View v); // I am getting error "Cannot resolve symbol". 

     return(true); 
    case R.id.reset: 
     //add the function to perform here 
     return(true); 
    case R.id.about: 
     //add the function to perform here 
     return(true); 
    case R.id.exit: 
     //add the function to perform here 
     return(true); 
} 
    return(super.onOptionsItemSelected(item)); 

我想知道我應該傳遞給函數的我是什麼樣的看法在打電話嗎?

回答

1

好像public void myLocation (View v){是點擊偵聽器,以便創建一個新的功能(只要你喜歡它命名),並在一個新的功能

public void getLocation(){ 
    MyMapSettings(MY_LOCATION); 
    showMyAddressOnMap(); 
    drawGeofencesAround(MY_LOCATION, false); 
} 

移動的myLocation代碼,並從任何地方調用它

public void myLocation (View v){ 
    getLocation(); 
    } 

case R.id.add:  
    getLocation();  
    return(true); 

,如果你不希望這件事不要再簡單地傳遞null

case R.id.add:  
     getLocation(null);  
     return(true); 
+1

謝謝,它的工作原理。 – jason

相關問題