无法从主项目访问变量?

huangapple go评论103阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2020年8月11日 00:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63344075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定