0

I’m trying to call MainActivity.my_method() from a different class, but I get:

non-static method my_method(Context) cannot be referenced from a static context.

MainActivity.java

public class MainActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        my_method(); //WORKS WELL
    }

    public void my_method(Context context) {
        context.startActivity( new Intent( Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse( "package:" + context.getPackageName() ) ) );
    }
}

My_view.java

class My_view extends SurfaceView implements SurfaceHolder.Callback {

    class My_viewThread extends Thread implements SensorEventListener, OnTouchListener{
       …
       private Context mContext;
       …
       public void my_calling_method() { 
           MainActivity.my_method(mContext);
       }
       .... 
                    

Any help will be very appreciated.

user20191130
  • 340
  • 1
  • 9
  • 1
    You're doing wrong way, don't create new instance of an Activity as the activity itself need to be created and handle by the framework. In your case, calling to get the package name cause null exception because there is no context info. Take a look at this question, it will help you to understand what is going on: https://stackoverflow.com/questions/44582309/why-cant-we-create-an-activity-using-new-keyword – Lạng Hoàng Mar 02 '21 at 04:44

2 Answers2

1

You should never create an instance of an activity yourself, that is something that Android has to handle for you.

Don't do:

 MainActivity obj = new MainActivity ();

Considering this is what you said you have:

  class My_view extends SurfaceView implements SurfaceHolder.Callback {

  class My_viewThread extends Thread implements SensorEventListener, OnTouchListener{
   …
   private Context mContext; // <-- you have context here, so why not just use it ?
   … 
   public void my_calling_method() { 
        context.startActivity( new Intent( Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse( "package:" + context.getPackageName() ) ) );
   }
   .... 
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
1

You could pass context to My_view Class like below:

public class MyView {
    Context mContext;
    MyView(Context context){
        mContext = context;
    }
    public void my_calling_method() {
        try
        {
            SqliteTestActivity obj = new SqliteTestActivity ();
            obj.my_method(mContext); 
        }
        catch (Exception e)
        {
            Log.d("tis==>>",""+e.getLocalizedMessage());
        }
    }
}

Then, in MainActivity change the method signature like below:

public void my_method(Context context) {
    context.startActivity( new Intent( Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse( "package:" + context.getPackageName() ) ) );
}
NRUSINGHA MOHARANA
  • 1,489
  • 8
  • 13
  • why are you also creating an instance of an activity yourself ? that's also incorrect – a_local_nobody Mar 02 '21 at 05:35
  • I just add context parameter to overcome null pointer exception . Ideally, it is not recommended to create object of Activity class just like conventional way of object creation. – NRUSINGHA MOHARANA Mar 02 '21 at 06:13