有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法解析安卓中来自其他类的方法调用?

我正在进行一项活动

public class SettingActivity extends Activity {

    // here i am having my function
    // Boolean
    public static boolean ReadBoolean(Context context, final String key,
                                      final boolean defaultValue) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        return settings.getBoolean(key, defaultValue);
   }
}

但是在我的另一节课上

public class MyServices extends Service
{
    public void onCreate()
    {
        Noti_flag= SettingActivity.ReadBoolean(context, "NotiOnOff", true);
        super.onCreate();
    }

}

ReadBoolean is showing cannot resolve method 请帮我解决这个问题,谢谢


共 (4) 个答案

  1. # 1 楼答案

    我想你的意思是:

    SingleActivity.ReadBoolean(...
    
  2. # 2 楼答案

    您正在使用SettingActivity而不是SingleActivity

  3. # 3 楼答案

    使用下面的代码从服务类调用方法

       public class MyServices extends Service
        {
            public void onCreate()
            {
                Noti_flag= SettingActivity.ReadBoolean(getApplicationContext(), "NotiOnOff", true);
                super.onCreate();
            }
        }
    

    您需要传递getApplicationContext()上下文

  4. # 4 楼答案

    我想你是打错了而不是SingleActivity你想用SettingActivity。你可以试试:

    Noti_flag = SingleActivity.ReadBoolean(context, "NotiOnOff",true);
    

    顺便说一句,如果您遵循javabean命名约定,您可能更喜欢使用:

    readBoolean instead of `ReadBoolean`
    

    **编辑**** 如果Noti_标志是局部变量,则还需要定义其类型:

    boolean Noti_flag = SingleActivity.ReadBoolean(context, "NotiOnOff",true);