-2

I have a MainActivity class where I have implemented a main function. This main function calls a non-activity class by passing a context (unable to implement). In the non activity class, I want to access a file from the assets folder (which requires a context, hence the passing of one) and print it in the LogCat. This is how I am trying to do it

MainActivity:

public class MainActivity extends AppCompatActivity
{

    public static void main(String[] args)
    {
        DiseaseAlgo(this).Parser();//Error. Unable to pass context
        return;
    }

    //etc
}

NonActivity Class:

package com.iotaconcepts.aurum;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.FileReader;
import java.io.IOException;

public class DiseaseAlgo
{
    static Context mContext;
    public DiseaseAlgo(Context context)
    {
        mContext = context;
    }

    public static void Parser() throws IOException
    {
        AssetManager as=mContext.getAssets();
        String parse=as.open("symp.txt").toString();
        String[] xx=parse.split("\t");
        for(String i:xx)
            Log.w("DiseaseAlgo", i);
    }
}

I'm unable to figure out how to pass the context and call the 'DiseaseAlgo' method from this function. I keep getting:

package name.MainActivity.this cannot be referenced from static content

I need this to check the correctness of my text parsing and this is the only way to show it on the LogCat.

halfer
  • 19,824
  • 17
  • 99
  • 186
gabbar0x
  • 4,046
  • 5
  • 31
  • 51
  • This is really not the way to go, if you have problem debugging - solve it, don't add another problem. – SuperFrog Nov 27 '15 at 22:26
  • If this is not the way to go, please suggest another method and do not downvote the question if you do not have a solution. Thanks – gabbar0x Nov 27 '15 at 22:27
  • 1
    Actually, I didn't downvote. And I didn't try to solve your question, because if you try and develope for Android, you should make a minimum effort of understanding what you're doing. – SuperFrog Nov 27 '15 at 22:30
  • Do you have any idea how I can resolve this? – gabbar0x Nov 27 '15 at 22:35

1 Answers1

2

The problem is that you are using the word this from a static function. Since the function is static, the class may or may not have an instance. What is calling that function?

If your instance of your activity is calling your Main() then pass it's context (this) as an arguement so the static function has access to it.

I would guess Main() is not from android's functions right? Normaly in a main() function you should initialize objects before passing them around so this does not make sense, but I don't think android works that way.

TomTsagk
  • 1,474
  • 9
  • 23
  • well you have to answer my question, what is calling `Main()` ? – TomTsagk Nov 28 '15 at 00:23
  • I'm so sorry for the type. It's 'main' – gabbar0x Nov 28 '15 at 00:24
  • @bholagabbar as far as I know `main()` is not the same in C++/Java as in Android Java, why are you using `main()` ? This is an android project right? – TomTsagk Nov 28 '15 at 00:28
  • Yes it is. I just wanted main to call the non-activity class, print the data on the logcat and end – gabbar0x Nov 28 '15 at 00:29
  • 1
    @bholagabbar I'm not sure why you'd call `main()`, try putting the code on an `onCreate()` method, in there your activity has an instance, so you can pass it a `this` arguement. – TomTsagk Nov 28 '15 at 00:30