Run MainActivity method in another class

0

My goal is to execute the activateFunction () method using a Thread. Pressing a button is called the TactivarBroucast () method and after 5s it calls the run () method of the Timer class and this is responsible for executing the Thread and the Thread is responsible for executing the activateFunction (). Before I had this error (there's the class Activity_Calls COMPLETE) I was suggested to see this link. I did not manage to stay like in the first answer I get an error when putting the:

 @Override
          public void handleMessage(Message msg) {
              // dice que el metodo no pertenece a la clase padre
          }

I say this because I DO NOT KNOW IF IT WILL BE WELL THIS:

public class MyTherad extends Thread {
 public Handler handler;
private Activity_llamadas activity_llamadas;
@Override
public void run() {
        Looper.prepare();
        handler = new Handler() {
            @Override
            public void publish(LogRecord record) {
            }          

            @Override
            public void flush() {
            }
            @Override
            public void close() throws SecurityException {
            }
        };

        activity_llamadas = new Activity_llamadas();
        activity_llamadas.activarFuncion(); //<----LINEA 47
        Log.d("metodo", "Clase MyTherad");
        Looper.loop();
    }
}

I did not get the error mentioned above. I do not know if it is fixed but it does not work out anymore. Now I get this new error:

03-03 11:50:57.011 30704-30820/com.example.andry.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-4724
                                                                             Process: com.example.andry.myapplication, PID: 30704
                                                                             java.lang.NullPointerException
                                                                                 at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
                                                                                 at android.content.ComponentName.<init>(ComponentName.java:77)
                                                                                 at com.example.andry.myapplication.Activity_llamadas.activarFuncion(Activity_llamadas.java:138)
                                                                                 at com.example.andry.myapplication.MyTherad.run(MyTherad.java:47)

Method to activate Function () of the Activity_Calls class (Unique and main activity):

public  void activarFuncion(){
   ComponentName mReciver= new ComponentName(this,Call_Reciver.class);//<--LINEA 138
    PackageManager pm=getPackageManager();
    pm.setComponentEnabledSetting(mReciver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
}

Timer Class:

public class Temporisador extends TimerTask {


Thread myTherad;
@Override
 public void run(){
         myTherad= new MyTherad();
         myTherad.start();
    Log.d("metodo","clase Temporizador");

   }
}

I WOULD REALLY THANK HIS HELP ... THANK YOU IN ADVANCE

    
asked by Andry_UCI 03.03.2018 в 18:28
source

1 answer

0

Now solve the problem. Create an instance of my Activity

 private static Activity_llamadas activity_llamadas;

Then in the onCreate () method

 @Override
 public void onCreate(Bundle savedInstanceState) {
    //.....
    activity_llamadas=this;
}

Create a method that returns an instance:

public static Activity_llamadas getmInstanceActivity(){
    return activity_llamadas;
}

and then call the method in the other class in this way:

   Activity_llamadas.getmInstanceActivity().activarFuncion();

For more details see this link

    
answered by 04.03.2018 / 01:43
source