在片段中的静态函数中使用对象创建Hashmap

时间:2016-04-01 14:19:03

标签: java android hashmap

我正在尝试在片段中的静态函数中创建一个hashmap。 但是我在创建hashmap时遇到错误,说我的函数不能是静态的。你能告诉我怎样才能让我的函数保持静态并将我的hashmap保留在里面?

我的片段:

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());

         }
}

错误:无法从静态上下文引用它 在这一行:

textviewresmap.put("textview" + i, new TextView(this.getContext()));

2 个答案:

答案 0 :(得分:1)

向您的班级添加静态变量:

private static Activity activity;

在你的onCreate()中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    activity = getActivity();

}

然后你可以写:

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());

    }
}

答案 1 :(得分:0)

Java中的

this是指向实例的指针,因此它不能与静态字段一起使用,因为在调用静态方法时this可能不存在。

对于访问静态字段,最好从ClassName中调用它:

ClassName.myMap.put(a, b);

您的方法无法访问this指针,您需要将其从其他位置传递给该方法: public static void Result(Context context){并摆脱this.getContext()

我给你的解决方案仍然非常危险,因为你传递给方法的上下文可能会改变消失,你会遇到意外的失败。

如果您有时间,请阅读此材料:

https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Java: when to use static methods