Android:无法从另一个类访问公共静态函数或公共变量

时间:2023-01-01 15:59:57

Here is the declaration in 'MainActivity.java'

这是'MainActivity.java'中的声明

private static String competition = null;

I've created a setter function which adds value to it.

我已经创建了一个为其增加价值的setter函数。

public static void setCompetition(String competition1) {
    competition = competition1;
}

I've created a getter function to get the value in another class from the same package:

我已经创建了一个getter函数来从同一个包中获取另一个类的值:

public static String getCompetition() {
    return competition;
}

However it returns null.

但是它返回null。

Here is how I tried to use it in a function public void onReceive(Context context, Intent intent) {

以下是我尝试在函数public void onReceive(Context context,Intent intent)中使用它的方法{

with in the class AlarmNotify which extends BroadcastReceiver:

在类AlarmNotify中扩展BroadcastReceiver:

       final String competition = MainActivity.getCompetition();

2 个解决方案

#1


0  

I think you are not invoking setCompetition anywhere. To validate that, try with this piece of code -

我想你不是在任何地方调用setCompetition。要验证这一点,请尝试使用这段代码 -

public static void setCompetition(String competition1) {
    System.out.println("Set competition to "+competition1);
    competition = competition1;
}

If you do not see any printed message, then setCompetition is not being invoked. Ensure that this is being invoked.

如果您没有看到任何打印的消息,则不会调用setCompetition。确保正在调用它。

#2


0  

Here is the mistake I did: I was calling a network based async task, which takes few seconds to retrieve the data. It works in the background.

这是我做的错误:我正在调用基于网络的异步任务,这需要几秒钟来检索数据。它在后台运行。

I was calling another function which was trying to access these values. It was being fired instantaneously, even before the async task could run. That is why it was returning null.

我正在调用另一个试图访问这些值的函数。甚至在异步任务运行之前,它就被即时触发。这就是它返回null的原因。

Async task hadn't returned those values.

异步任务没有返回这些值。

Ultimately I put the call to the other function in the postExecute method of the Async.

最后,我将调用放在Async的postExecute方法中的另一个函数中。

Hope that helps anyone who makes the same mistake as I did.

希望能帮助那些犯同样错误的人。

#1


0  

I think you are not invoking setCompetition anywhere. To validate that, try with this piece of code -

我想你不是在任何地方调用setCompetition。要验证这一点,请尝试使用这段代码 -

public static void setCompetition(String competition1) {
    System.out.println("Set competition to "+competition1);
    competition = competition1;
}

If you do not see any printed message, then setCompetition is not being invoked. Ensure that this is being invoked.

如果您没有看到任何打印的消息,则不会调用setCompetition。确保正在调用它。

#2


0  

Here is the mistake I did: I was calling a network based async task, which takes few seconds to retrieve the data. It works in the background.

这是我做的错误:我正在调用基于网络的异步任务,这需要几秒钟来检索数据。它在后台运行。

I was calling another function which was trying to access these values. It was being fired instantaneously, even before the async task could run. That is why it was returning null.

我正在调用另一个试图访问这些值的函数。甚至在异步任务运行之前,它就被即时触发。这就是它返回null的原因。

Async task hadn't returned those values.

异步任务没有返回这些值。

Ultimately I put the call to the other function in the postExecute method of the Async.

最后,我将调用放在Async的postExecute方法中的另一个函数中。

Hope that helps anyone who makes the same mistake as I did.

希望能帮助那些犯同样错误的人。