【问题标题】:Android Studio - reference getAssets() as static contextAndroid Studio - 将 getAssets() 引用为静态上下文
【发布时间】:2016-08-17 17:34:41
【问题描述】:

我正在尝试创建一个读取 Json 值的类。 MainActivity 类中的函数工作正常,但是,如果我尝试创建一个单独的类文件,我会收到以下错误:non-static method getAssets() cannot be referenced as stain context

我该如何解决这个问题?

public class jsonClass extends AppCompatActivity {

    private static Context mContext;

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

            InputStream is = getAssets().open(file); //ERROR

            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;

    }

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

        JSONObject object = null;
        try {

            // Seleziona il file Json
            object = new JSONObject(loadJSONFromAsset(jsonFile));

            //Oggetto JSON per ogni anno
            JSONObject getEra = object.getJSONObject(anni);

            //Lista Livelli per ogni anno
            JSONObject getLevel = getEra.getJSONObject(level);

            //Ritorna Valore scelto per ogni Livello
            value = getLevel.getString(getValue);

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

        return value;
    }
}

【问题讨论】:

  • 你在哪里得到错误?显示该代码
  • 在声明类时尝试删除 static 关键字:loadJSONFromAsset
  • 出现错误:InputStream is = getAssets().open(file); //ERROR
  • 欢迎来到 Stack Overflow!您能否在解决问题的努力下,在内容中提供更好的标题和更详细的信息?
  • 也欢迎您。我觉得内容有足够详细的信息,而且标题正是问我需要什么

标签: java android static-methods


【解决方案1】:

如果您使用有效上下文初始化静态mContext,您应该能够编写

       InputStream is = mContext.getAssets().open(file);

【讨论】:

  • 我已经尝试过了,但是当我调用类时应用程序崩溃String text = jsonClass.getJsonValue("allLevels.json", "90s", "level1", "text");
【解决方案2】:

您已将 loadJSONFromAsset 类声明为静态类。这是创建错误,因为当您调用方法 getAssets() 时,InputStream 类不是静态的。 检查此链接以获取更多信息

https://developer.android.com/reference/java/io/InputStream.html

从链接可以看出,InputStream 类其实是 public abstract class InputStream extends Object implements Closeable。由于这个类不是静态的,你不能在静态类中使用它的方法。

【讨论】:

  • 好的,谢谢,那还有其他方法吗?
【解决方案3】:

声明你的方法也使用public而不是static

public static String loadJSONFromAsset(Context mContext, String file){

        InputStream is = mContext.getAssets().open(file); 
}

【讨论】:

  • 如果我这样做,我会在 MainActivity 上收到错误 make getJsonValue static
  • 如果是静态的,请执行以下操作:
  • 它崩溃了java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.g
  • 试试这个:context.getResources().getAssets()
【解决方案4】:

问题在于您处于静态环境中。为了摆脱它,在您的主要活动中创建您的类 jsonClass 的实例,并摆脱您 jsonClass 中的所有“静态”。然后在 jsonClass 的实例上调用 getJsonValue。

在主类中: jasonClass jclass= new jasonClass(); jclass.getJsonValue(....);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 2020-05-24
    相关资源
    最近更新 更多