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:
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");
}
}
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("");
}
}
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("");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With