英文:
Can't access variable from main project?
问题
I cannot access the hi
variable from my library class. Why? Check it out:
I have this inteface in my library:
interface ContextAccessor {
fun getApplicationContext(): Application?
}
And this code as well:
class SomeLibraryClass {
private var mContextAccessor: ContextAccessor?
String extractedHi = null
fun setContextAccessor(contextAccessor: ContextAccessor?) {
mContextAccessor = contextAccessor
}
fun someOtherMethod() {
mContextAccessor?.getAppContext()?.let { nonNullContext ->
// use nonNullContext here
extractedHi = nonNullContext.hi; // i get error here!
}
}
}
And this class in my project:
public class MyActivity extends Activity implements MyActivity.ContextAccessor {
private SomeLibraryClass someLibraryClassInstance = SomeLibraryClass();
public String hi = "hi";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ContextAccessor reference is set to some library class
someLibraryClassInstance.setContextAccessor(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Super important!
someLibraryClassInstance.setContextAccessor(null);
// OR create some method like `someLibraryClassInstance.removeContextAccessor(this)`
}
@Override
public Application getApplicationContext() {
return super.getApplication();
}
}
英文:
I cannot access the hi
variable from my library class. Why? Check it out:
I have this inteface in my library:
interface ContextAccessor {
fun getApplicationContext(): Application?
}
And this code as well:
class SomeLibraryClass {
private var mContextAccessor: ContextAccessor?
String extractedHi = null
fun setContextAccessor(contextAccessor: ContextAccessor?) {
mContextAccessor = contextAccessor
}
fun someOtherMethod() {
mContextAccessor?.getAppContext()?.let { nonNullContext ->
// use nonNullContext here
extractedHi = nonNullContext.hi; // i get error here!
}
}
}
And this class in my project:
public class MyActivity extends Activity implements MyActivity.ContextAccessor {
private SomeLibraryClass someLibraryClassInstance = SomeLibraryClass();
public String hi = "hi";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ContextAccessor reference is set to some library class
someLibraryClassInstance.setContextAccessor(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Super important!
someLibraryClassInstance.setContextAccessor(null);
// OR create some method like `someLibraryClassInstance.removeContextAccessor(this)`
}
@Override
public Application getApplicationContext() {
return super.getApplication();
}
}
答案1
得分: 0
将ContextAccessor
接口添加hi
属性:
interface ContextAccessor {
val hi: String
// ...
}
在MyActivity
中实现getHi()
方法:
@NotNull
@Override
public String getHi() {
return hi;
}
在你的库类SomeLibraryClass
中,你可以像下面这样访问它:
var extractedHi: String? = null
fun someOtherMethod() {
extractedHi = mContextAccessor?.hi
}
英文:
Add hi
property to the ContextAccessor
interface:
interface ContextAccessor {
val hi: String
// ...
}
And in MyActivity
implement getHi()
method:
@NotNull
@Override
public String getHi() {
return hi;
}
In your library class SomeLibraryClass
you can access to it like the following:
var extractedHi: String? = null
fun someOtherMethod() {
extractedHi = mContextAccessor?.hi
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论