我遵循了aws指南,但却为getApplicationContext()
收到了错误。
package com.example.wcyv3.stylish;
import android.content.Context;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
public class ManagerClass {
public CognitoCachingCredentialsProvider getCredentials() {
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(),// Context
"ap-southeast-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Identity pool ID
);
return credentialsProvider;
}
}
答案 0 :(得分:0)
在活动类中使用此方法或在方法中使用类型为Context
的参数。
public CognitoCachingCredentialsProvider getCredentials(Context context){
return new CognitoCachingCredentialsProvider (context, "ap-southeast-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxx" // Identity pool ID
);
答案 1 :(得分:0)
这是因为你的类中没有属于Activity,Fragment,Application等的getApplicationContext()
方法。要解决这个问题,你可以通过构造函数传递上下文或者您可以将方法设置为静态,然后将上下文作为参数传递。
通过构造函数:
public class ManagerClass {
private Context context;
public ManagerClass(Context context) {
this.context = context;
}
public CognitoCachingCredentialsProvider getCredentials() {
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
context,
"ap-southeast-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Identity pool ID
);
return credentialsProvider;
}
}
或通过静态方法的参数:
public class ManagerClass {
public static CognitoCachingCredentialsProvider getCredentials(Context context) {
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
context,
"ap-southeast-1:xxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Identity pool ID
);
return credentialsProvider;
}
}