android - 我应该将哪个 View 对象传递给以 View 对象作为菜单参数的函数?

标签 android function view menu

这是我应该调用的函数

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

我想知道我应该将哪种 View 传递给我正在调用的函数?

最佳答案

似乎 public void myLocation (View v){ 是一个点击监听器,因此创建一个新函数(随意命名)并将 myLocation 的代码移动到一个新功能

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

然后像这样在任何地方调用它

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

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

如果你不想要这一切,那么只需传递 null

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

关于android - 我应该将哪个 View 对象传递给以 View 对象作为菜单参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44247143/

相关文章:

android - 错误 :Execution failed for task ':app:processDebugResources' when using font folder inside res

javascript - 在执行渲染方法之前数据不可用 - React Native

java - 如何从 Vaadin 8 URL 中删除 "#!"?

cocoa - 使用drawRect方法绘图 - Cocoa

cocoa-touch - 使用 modalPresentationStyle 显示 View

android - 主页和后退按钮在 android 模拟器像素 2 和像素 3 api 中不起作用

android - 在 Android 中解码 JSON base64 数据(来自 PHP 的 base64_encode)?

c++ - 在 C++ 程序中使用多个 .cpp 文件?

c++ - 我正在创建一个函数类型程序,并且不会停止说 : (. text+0xe0): undefined reference to

java - Android Socket 客户端接收数据