简体   繁体   English

用Java自动构造的对象

[英]Object constructed automatically in java

[EDIT] [编辑]

If getters in RolloParser is deleted, the problem is solved. 如果删除了RolloParser中的吸气剂,则可以解决问题。 But why getter method make the class constructed automatically? 但是,为什么用getter方法使类自动构建?

I have a class named RolloParser you can see below. 我有一个名为RolloParser的类,您可以在下面看到。 I use this class to parse a JSON file in my assets folder. 我使用此类来解析资产文件夹中的JSON文件。

Problem is when I check this class is not null in my application class, I see that RolloParse has already been constructed. 问题是,当我在应用程序类中检查此类不为null时,看到RolloParse已被构造。 I wonder why? 我想知道为什么?

Line 7 never works but parser object is not null. 第7行从不工作,但解析器对象不为null。

Application class method 应用类别方法

1- private RolloParser parser=null;
2- public RolloParser getRolloParser(){
3-
4-  //in first run, parser object is not null ???
5-
6-  if(parser == null){
7-     parser = new RolloParser(mContext);
8-  }
9-      
10-  return parser;
11- }

Parser Class 解析器类

import java.io.IOException;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class RolloParser  {

    private String path ="www/android-device/rollo.json";
    private String extension =".ttf";
    private HashMap<String, String> iconMap;
    private HashMap<String, String> fontMap;

    public RolloParser(){
        System.err.println();
    }

    public RolloParser(Context context) {
        try {
            iconMap = new HashMap<String, String>();
            fontMap = new HashMap<String, String>();

            JSONObject objTop;
            try {
                objTop = new JSONObject(AndroidUtils.ReadFromfile(path, context));

                String fonts = objTop.getString("fonts");

                JSONObject objFonts = new JSONObject(fonts);
                JSONArray icons = objFonts.getJSONArray("icons");
                JSONArray others = objFonts.getJSONArray("others");


                for (int i = 0; i < icons.length(); i++) {
                    JSONObject objIcon = new JSONObject(others.get(i).toString());
                    iconMap.put(objIcon.get("family").toString(),objIcon.get("file").toString()+extension);
                };

                for (int i = 0; i < others.length(); i++) {
                    JSONObject objIcon = new JSONObject(others.get(i).toString());
                    fontMap.put(objIcon.get("family").toString(),objIcon.get("file").toString()+extension);
                }

                for (int i = 0; i < others.length(); i++) {
                    Log.d("json", others.get(i).toString());
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public HashMap<String, String> getIconMap() {
        return iconMap;
    }

    public HashMap<String, String> getFontMap() {
        return fontMap;
    }
}

您必须一直在初始化的地方调用getRolloParser()方法。确保在getRolloParser中放置一个断点并检查解析器是否为null,然后将其初始化。

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

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