Reputation: 681
I'm trying to create a hashmap in a static function in a fragment. But I get an error creating the hashmap saying my function cannot be static. Can you tell me how can I do to keep my function static and keeping my hashmap inside?
My fragment :
public class AddMatriceResult extends Fragment {
private static int i = 0, j = 0, l = 0;
private static int o = MatriceActivity.n * MatriceActivity.m;
private static HashMap<String, LinearLayout> layoutresmap = new HashMap<String, LinearLayout>();
private static HashMap<String, TextView> textviewresmap = new HashMap<String, TextView>();
private static List<LinearLayout> layoutreslist;
private static List<TextView> textviewreslist;
private static TextView noAddMatriceResult;
private static LinearLayout layoutResultCalc;
public AddMatriceResult() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.add_matrice_result, container, false);
layoutResultCalc = (LinearLayout) v.findViewById(R.id.LayoutMatriceRes);
noAddMatriceResult = (TextView) v.findViewById(R.id.noAddMatriceResult);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public static void Result(){
if(AddMatriceCalc.flag == 1)
{
noAddMatriceResult.setVisibility(View.GONE);
layoutResultCalc.setVisibility(View.VISIBLE);
for(i=0;i<MatriceActivity.m;i++) {
layoutresmap.put("layout" + i, new LinearLayout(this.getContext()));
}
layoutreslist = new ArrayList<LinearLayout>(layoutresmap.values());
for(i=0;i<o;i++) {
textviewresmap.put("textview" + i, new TextView(this.getContext()));
}
textviewreslist = new ArrayList<TextView>(textviewresmap.values());
}
}
Error : this cannot be referenced from a static context on this line :
textviewresmap.put("textview" + i, new TextView(this.getContext()));
Upvotes: 1
Views: 281
Reputation: 8362
Add a static variable to your class:
private static Activity activity;
In your onCreate():
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
}
Then you can write:
public static void Result() {
if (AddMatriceCalc.flag == 1) {
noAddMatriceResult.setVisibility(View.GONE);
layoutResultCalc.setVisibility(View.VISIBLE);
for (i = 0; i < MatriceActivity.m; i++) {
layoutresmap.put("layout" + i, new LinearLayout((Context) activity));
}
layoutreslist = new ArrayList<LinearLayout>(layoutresmap.values());
for (i = 0; i < o; i++) {
textviewresmap.put("textview" + i, new TextView((Context) activity));
}
textviewreslist = new ArrayList<TextView>(textviewresmap.values());
}
}
Upvotes: 1
Reputation: 6223
this
in Java is a pointer to instance, to itself, so it can't be used with static fields, as this
might not exists when you call static method.
For accessing static fields it's a good practice to call it from ClassName:
ClassName.myMap.put(a, b);
Your method can't access this
pointer, you need to pass it to the method from elsewhere:
public static void Result(Context context){
and get rid of this.getContext()
The solution I gave you is still very dangerous, as the context you will pass to the method might change of disappear and you will get unexpected failures.
Read this material when you have some time:
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Java: when to use static methods
Upvotes: 0