java 无法解析android studio中的某些符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27433515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:48:09  来源:igfitidea点击:

Cannot resolve certain symbols in android studio

javaandroidclass

提问by ACluelessProgramer

I've been having this error where this class(DataProvider) for my project that I just created will not seem to accept .add or .put. But my MainActivity class accepts them just fine. I've already done the Invalidate Caches and Restart. Can someone explain to me why this is happening and what I can do to fix it?

我一直有这个错误,我刚刚创建的项目的这个类(DataProvider)似乎不接受 .add 或 .put。但是我的 MainActivity 类可以很好地接受它们。我已经完成了无效缓存和重启。有人可以向我解释为什么会发生这种情况以及我可以做些什么来解决它?

DataProvider:

数据提供者:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataProvider {
public static HashMap<String, List<String>> getInfo()

HashMap<String, List<String>> MoviesDetails = new HashMap<String, List<String>>();

List<String> Action_Movies = new ArrayList<String>();
 //'.add'  would be an error in red.  Cannot resolve symbol 'add'. 
Action_Movies.add("");
List<String> Romantic_Movies = new ArrayList<String>();

Romantic_Movies.add("");
List<String> Horror_Movies = new ArrayList<String>();
Horror_Movies.add("");
List<String> Comedy_Movies = new ArrayList<String>();
Comedy_Movies.add("");

}

}

MainActivity:

主要活动:

import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class MainActivity extends Activity {
HashMap<String, List<String>> Movies_categories;
List<String> Movies_list;
ExpandableListView Exp_list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<String> Test = new ArrayList<String>();
    Test.add("Test");



}

}

}

回答by Kelevandos

You are missing a semicolon at the end of this line:

您在此行的末尾缺少分号:

public static HashMap<String, List<String>> getInfo()

Which makes the code analysed go crazy and not see your objects correctly as their types. Just add the semicolon.

这使得分析的代码变得疯狂并且无法将您的对象正确地视为它们的类型。只需添加分号。

Also, you will probably want to remove the parenthesis at the end of the line, as it suggests it it a method. So do something like this:

此外,您可能希望删除行尾的括号,因为它表明它是一种方法。所以做这样的事情:

public static HashMap<String, List<String>> getInfo;

Or this:

或这个:

public static HashMap<String, List<String>> getInfo(){
    //Some actual code here, which returns a HashMap
};

Now I see that your whole class tructure is messed up. Try the below code:

现在我看到你的整个班级结构都搞砸了。试试下面的代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataProvider {
public static HashMap<String, List<String>> getInfo;

    public DataProvider() { 

        HashMap<String, List<String>> MoviesDetails = new HashMap<String, List<String>>();

        List<String> Action_Movies = new ArrayList<String>();
        Action_Movies.add("");
        List<String> Romantic_Movies = new ArrayList<String>();

        Romantic_Movies.add("");
        List<String> Horror_Movies = new ArrayList<String>();
        Horror_Movies.add("");
        List<String> Comedy_Movies = new ArrayList<String>();
        Comedy_Movies.add("");

    }

}

回答by Joseph Webber

On another side note, consider creating your variables outside of the constructor and initializing them inside the constructor.

另一方面,请考虑在构造函数之外创建变量并在构造函数内部初始化它们。

public class DataProvider {
    public static HashMap<String, List<String>> getInfo;
    HashMap<String, List<String>> movieDetails;
    List<String> actionMovies;
    List<String> romanticMovies;
    List<String> horrorMovies;
    List<String> comedyMovies;

    public DataProvider() {
        movieDetails = new HashMap<String, List<String>>();
        actionMovies = new ArrayList<String>();
        actionMovies.add("");
        romanticMovies = new ArrayList<String>();
        romanticMovies.add("");
        horrorMovies = new ArrayList<String>();
        horrorMovies.add("");
        comedyMovies = new ArrayList<String>();
        comedyMovies.add("");
    }
}

回答by Cesar Alvarado Diaz

These lines may have been added inside the class but outside the constructor or method. Make sure the instructions are within a method.

这些行可能已添加到类内部,但在构造函数或方法之外。确保说明在方法中