-3

i had this in my class: private Context mCtx = getBaseContext();

then the following code is my other class from which i want to use a file dictionary.txt from assets folder, I get IO Exception. Please Help

public class RandomStringGenerator {

    private final char[] generatedLetters = generate4().toCharArray();
        private int formedWordNo = 0;
        private final Set <String> formedWords = wordDictAvail(generatedLetters.toString());


        public Set<String> wordDictAvail (String inword){
            Set<String> set = new HashSet<String>();
            int wa = 0;
                 int dictWordLn;
                 String dictWord;

                 try {
                        is =mCtx.getAssets().open("dictionary.txt");

                     Scanner scanner=new Scanner("is");

                 while (scanner.hasNextLine()) {
                     String currChar;
                int i = 1;
                     dictWord = scanner.nextLine().trim();
            dictWordLn = dictWord.length();
                for (int a = 0; a <= dictWordLn-1 ; a++){
                    currChar = Character.toString(dictWord.charAt(a));
                    if (inword.contains(currChar)){
                        i =i*1;
                                     } else {i=i*2;}
                    }
                if (i==1){
                    set.add(dictWord);
                                     wa++;
                                     }

                                                                    }
                 scanner.close();
                 } catch(IOException e) {
                     e.printStackTrace();
                 }
}
Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
Neo- Ex
  • 13
  • 2

1 Answers1

0

in your MainActivity or any class which extends from Activity declare a static Context

private static Context con; 

Assign in onCreate()

con = MainActivity.this;

From any other Activity reference this context like

Context presentContext = MainActivity.con

Let me know if that helps.

Skynet
  • 7,820
  • 5
  • 44
  • 80