【问题标题】:Can not resolve getAssets() method from inside a java file: Android无法从 java 文件中解析 getAssets() 方法:Android
【发布时间】:2017-03-23 04:52:27
【问题描述】:

我创建了一个文件 在assets文件夹中,现在我想从java类中读取文件并将其传递给同一类中的另一个函数,但由于某种原因我无法使用getAssest()方法。请帮忙!

    public void configuration()
        {
            String text = "";
            try {
                InputStream is = getAssets().open("config.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                text = new String(buffer);


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


        }

public IExtraFeeCalculator getExtraFeeCalculator()
    {
        if(efCalculator==null)
        {
            if(configuration(Context context) == "extrafeeCalculaotor")
            {
                String className = System.getProperty("extraFeeCalculator.class.name");
                try {
                    efCalculator = (IExtraFeeCalculator)Class.forName(className).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }

        }
        return efCalculator;
    }

【问题讨论】:

  • 你需要有 Context 才能 getAssets() 像 context.getAssets()
  • 您可以使用 this.getAssets() ,我们需要 this/context 以便 android 系统知道您在哪里使用资产
  • 您需要使用有关该类的特定上下文。如果它是非活动类,则将Context context 作为configuration(Context context) 方法中的参数传递并用作InputStream is = context.getAssets().open("config.txt");
  • 我可以在任何地方使用这个参数吗?这个 Context 上下文实际上是做什么的?
  • 是的,你会的。

标签: android android-assets


【解决方案1】:

使用单参数上下文更改您的方法 .... 从调用此方法的位置传递上下文..

public void configuration(Context context)
    {
        String text = "";
        try {
            InputStream is = context.getAssets().open("config.txt");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            text = new String(buffer);


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


    }

现在是的,我认为你不知道 java 结构...

假设你有这个 YOUR_CLASS_NAME.java

public void YOUR_CLASS_NAME{

Context context;

YOUR_CLASS_NAME(Context context){

           this.context=context;
}

public void configuration(Context context)
        {
            String text = "";
            try {
                InputStream is = getAssets().open("config.txt");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                text = new String(buffer);


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


        }

public IExtraFeeCalculator getExtraFeeCalculator()
    {
        if(efCalculator==null)
        {
            if(configuration(context) == "extrafeeCalculaotor")
            {
                String className = System.getProperty("extraFeeCalculator.class.name");
                try {
                    efCalculator = (IExtraFeeCalculator)Class.forName(className).newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }

        }
        return efCalculator;
    }

}

【讨论】:

  • 最重要的是你在哪里调用这个方法,如果你从Activity调用通过getApplicationContext(),如果Fragment用getActivity(),请告诉我是什么场景,谢谢。
  • 我不想在任何活动类中传递这个函数。我在java类中使用这个函数,我想在另一个函数中将文件信息作为字符串传递给同一个类,如果,配置()==“....”然后这样做{}现在什么我应该在配置方法中作为参数传递吗?
  • 您的 java 类之一必须与 Activity 或 Fragment 通信,如果您需要更多帮助,请从其中的哪个 java 类中调用此方法,为其放置一个单参数构造函数从您调用此方法的位置。
  • 请看我编辑的代码,现在,我想将字符串传递给一个新函数 getExtraFeeCalculator,在这种情况下,我的配置方法的参数应该是什么?
  • 我现在已经更新了答案,现在问题是你在哪里使用这个类,其中 Activity 和 Fragment...Go with this ...YOUR_CLASS_NAME obj = new YOUR_CLASS_NAME(getApplicationContext());然后是你调用的方法 obj.YOUR_METHOD_NAME..
【解决方案2】:

使用此代码

BufferedReader reader = null;
try {
 StringBuilder returnString = new StringBuilder();
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
        returnString.append(mLine );

    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

【讨论】:

    【解决方案3】:

    你应该试试

    getResources().getAssets().open("config.txt")

    而不是

    context.getAssets().open("config.txt");

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多