简体   繁体   English

内部嵌套静态类不能从另一个类调用

[英]Inner Nested Static Class cannot be called from another class

I have one class in a file named WeatherContract.java which has a static inner class, as follows: 我在名为WeatherContract.java的文件中有一个类,它具有一个静态内部类,如下所示:

public class WeatherContract {
...
...
    public static final class WeatherEntry implements BaseColumns {
    ...
    ...
    }
}

Now I am trying to call the inner class in another file called TestWeatherContract.java as follows 现在,我试图在另一个名为TestWeatherContract.java的文件中调用内部类,如下所示

...
import com.example.android.sunshine.app.data.WeatherContract;
...

public class TestWeatherContract extends AndroidTestCase {
...
    public void testBuildWeatherLocation() {
        Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION);
...

Now in the following line, the word "WeatherEntry" is marked in red and when I hover on the word I get the following error "Cannot resolve symbol 'WeatherEntry'. 现在,在下面的行中,单词“ WeatherEntry”用红色标记,当我将鼠标悬停在单词上时,出现以下错误“无法解析符号“ WeatherEntry”。

Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION);

Please note that I am not getting any error in the import statement, so I'm assuming that there are no errors in stating the path of the class. 请注意,导入语句中没有出现任何错误,因此我假设说明类的路径没有错误。

Also, I have another file called FetchWeatherTask.java. 另外,我还有一个名为FetchWeatherTask.java的文件。 I have the following import statement at the beginning of the file: 我在文件开头有以下import语句:

import com.example.android.sunshine.app.data.WeatherContract.WeatherEntry;

In this case, in the import statement again the word "WeatherEntry" is marked in red and I get the error "Cannot resolve symbol 'WeatherEntry'". 在这种情况下,再次在导入语句中将单词“ WeatherEntry”标记为红色,并且出现错误“无法解析符号'WeatherEntry'”。

Please help. 请帮忙。 I'm on Android Studio 1.5. 我在使用Android Studio 1.5。 I have posted this question earlier, but it was put on hold because I provided incomplete details. 我早些时候发布了这个问题,但是由于我提供的信息不完整,所以它被搁置了。 So I am posting it again with all details. 因此,我将再次发布所有详细信息。 I'm sorry if I have violated the rules of the community, I am new here. 很抱歉,如果我违反了社区规则,我是新来的。 Thank you. 谢谢。

WeatherEntry is a class and not a member. WeatherEntry是一类,而不是成员。 To access methods of WeatherEntry you still need an instance of it. 要访问WeatherEntry方法,您仍然需要一个实例。 Eg 例如

WeatherContract.WeatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION);

should be 应该

WeatherEntry weatherEntry = new WeatherContract.WeatherEntry();
weatherEntry.buildWeatherLocation(TEST_WEATHER_LOCATION);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM