Java 与';这';:方法不适用于参数,还改进了格式

Java 与';这';:方法不适用于参数,还改进了格式,java,android,this,Java,Android,This,你好 -A-在应用程序设置中: public class AppSettings { ... public static String getLogFileName(Context context){ SharedPreferences pref = context.getSharedPreferences(GPSLOGGER_PREF_NAME, 0); return pref.getString(LOG_FILE, ""); } -B-在主要活动中

你好

-A-在应用程序设置中:

public class AppSettings {
 ...
    public static String getLogFileName(Context context){
    SharedPreferences pref = context.getSharedPreferences(GPSLOGGER_PREF_NAME, 0);
        return pref.getString(LOG_FILE, "");
    }
-B-在主要活动中:

  AppLog.logString("Version A 05 Service Started with interval " + interval + ", Logfile name: " + AppSettings.getLogFileName(this));
==>工作正常

-但是在

public class SmsReceiver extends BroadcastReceiver

  File kmlFile = new File(folder.getPath(),AppSettings.getLogFileName(this));
相同的指令“AppSettings.getLogFileName(this)”出现错误(xxxx):

“此行有多个标记 -类型AppSettings中的方法getLogFileName(上下文)不适用于参数(SmsReceiver) -语法错误,插入“)”以完成VariableInitializer

为什么??存在简单的解决方法吗

编辑:添加“(”但不够

“类型AppSettings中的方法getLogFileName(Context)不适用于此行的参数(SmsReceiver)”

Missing')

File kmlFile = new File(folder.getPath(),AppSettings.getLogFileName(this); xxxx
试一试


活动
扩展了
上下文
。在
活动
类实例中,
引用当前实例,它是
上下文
的超类的实例

在您的
SmsReceiver
中,
引用当前实例,它是
广播接收器
超类的实例

您的
getLogFileName
方法需要类型为
Context
的参数,这就是为什么
可以从
活动
工作,但不能从
SmsReceiver
工作的原因

相反:

public class SmsReceiver extends BroadcastReceiver

  private Context mContext;

  public SmsReceiver(Context context)
  {
      mContext = context;
  }

  File kmlFile = new File(folder.getPath(),AppSettings.getLogFileName(mContext));
然后在您的
活动中

smsReceiver = new SmsReceiver(this);

该错误意味着您无法将
SmsReceiver
对象传递给
AppSettings.getLogFileName()
方法,因为它只接受
上下文
类或其子类的实例。SmsReceiver不是
上下文
类的直接或间接子类,因此您不能这样做。

您的语句是否包含在方法中(您的帖子中没有这种情况)?如果是的话,只有当它不是一个
static
one时,它才会起作用。两者都是“private void…”很好。请将答案标记为正确,以便其他人可以通过单击勾号找到答案。谢谢让开。在我的活动中,注册SmsReceiver IntentFilter=newintentfilter(“android.provider.Telephony.SMS_RECEIVED”);filter.setPriority(IntentFilter.SYSTEM\u高优先级);registerReceiver(新的SmsReceiver(),过滤器);因此,新错误“构造函数SmsReceiver()未定义”需要传递
上下文。这可能是
活动
或已知直接子类或已知间接子类列表中的其他内容。
smsReceiver = new SmsReceiver(this);