简体   繁体   English

Android Studio-创建类以在所有活动中读取Json本地文件

[英]Android Studio - Create Class to Read Json local file on all activity

I am trying to create a class that I can call on each activities in my app in order to read a local JSON file. 我正在尝试创建一个类,可以在我的应用程序中的每个活动上调用以读取本地JSON文件。

The function I have works fine if inside MainActivity.java however, when I try to create I get a crash in the app. 如果在MainActivity.java中,我拥有的功能会很好,但是当我尝试创建时,应用程序崩溃了。 I understand I have to create a Context in my java Class but I am trying everything and I cannot get it to work. 我知道我必须在我的java类中创建一个Context ,但是我正在尝试一切,但无法使其正常工作。

What am I missing? 我想念什么?

MainActivity.java MainActivity.java

Button botton2 = (Button) findViewById(R.id.bottone2);
        final TextView text2 = (TextView) findViewById(R.id.textView2);


        botton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String jsonValue = jsonClass.getJsonValue("file.json", "aa", "bob", "text");      

                testo.setText(jsonValue);

            }
        });

JSON Class: JSON类别:

public class jsonClass extends Application {

    private static Context mContext;
    private static InputStream is = null;
    //static AssetManager am = mContext.getResources().getAssets();

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
    }

    public static Context getContext(){
        return mContext;
    }


    public static String loadJSONFromAsset(String file)  {
        String json = null;
        try {

            is = jsonClass.getContext().getResources().getAssets().open(file);

            int size = is.available();

            byte[] buffer = new byte[size];

            is.read(buffer);

            is.close();

            json = new String(buffer, "UTF-8");


        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return json;

    }


    public static String getJsonValue(String jsonFile, String anni, String level, String getValue) {
        String value = null;

        JSONObject object = null;
        try {

            object = new JSONObject(loadJSONFromAsset(jsonFile));

            JSONObject getEra = object.getJSONObject(anni);

            JSONObject getLevel = getEra.getJSONObject(level);

            value = getLevel.getString(getValue);

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

        return value;
    }
}

Your Json class doesn't need to extend Application. 您的Json类不需要扩展Application。

Instead try : 而是尝试:

public class JsonClass {

   private static mContext;

   public JsonClass(Context context) {
       mContext = context;
    }

   ...
 }

Now instantiate it from your Main.java like this: 现在像这样从Main.java实例化它:

JsonClass json = new JsonClass(this);

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

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