title: Invoke method inside a Thread
body: <p>I just learn about Invoking Method from this <a href="https://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html" rel="nofollow">link</a>, and now I am trying to used it but I can't make it work with the logic I want. 
  I simply want to invoke a method from a Thread.</p>
  
  <p>I've used the below code and it work. But the problem here is the loop, 
  The loop have to list all the method from a class and check if the specific method is exist and then invoke it.</p>
  
  <pre><code>Class myclass = Class.forName("com.package.MainActivity");
  Method[] methods = myclass.getMethods();
  for (int i = 0; i &lt; methods.length; i++) {
      if (methods[i].getName().equals("methodName")) {
          Log.w(TAG, "Method Found!");
          methods[i].invoke("methodName", "string data", 0123, 123, "string data");
      }
  }
  </code></pre>
  
  <p>I want to make it simple with the below code, but it keeps on telling me that the method was not found. <code>ERROR: java.lang.NoSuchMethodException: methodName []</code></p>
  
  <pre><code>Class c=Class.forName("com.package.MainActivity");
  Method m=c.getMethod("methodName");
  Object t = c.newInstance();
  Object o= m.invoke(t,"string data", 0123, 123, "string data");
  </code></pre>
  
  <p>I also tried the below code, but it's also not working and the error says : <code>Can't create handler inside thread that has not called Looper.prepare()</code></p>
  
  <pre><code>Object carObj = Class.forName("com.package.MainActivity").newInstance();
  Method method = carObj.getClass().getMethod("methodName");
  method.invoke(method, "string data", 0123, 123, "string data");
  </code></pre>
  
  <p>I am doing all the above code with the below:</p>
  
  <p><strong>Main Class where the method should be call</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      static MyHelper myHelper;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btnStart = (Button)findViewById(R.id.btnStart);
  
          myHelper = new MyHelper(this);
          myHelper.StartThread();
  
      }
  
      /*The Method to Invoke*/
      public static void methodName(String data1, int var1, int var2, String data2){
          //some code here...
      }
  }
  </code></pre>
  
  <p><strong>Helper Class for starting Thread including some of my functions</strong></p>
  
  <pre><code>public class MyHelper{
      private Context context;
      public MyHelper(Context context){
         this.context = context;
      }
  
      public static void StartThread(){
          TheThread theThread = new TheThread(context);
          Thread t = new Thread(theThread);
          t.start();
      }
  
      //Some code below
  }
  </code></pre>
  
  <p><strong>The Thread where the method should be invoke</strong></p>
  
  <pre><code>public class TheThread implements Runnable{
  
      private Context context;
  
      public TheThread(Context context){
         this.context = context;
      }
  
      @Override
      public void run(){
          try{
  
              String className = context.getClass().getName(); //The Class Name including its package.
  
              //First Attempt
              /*Class myclass = Class.forName(className);
              Method[] methods = myclass.getMethods();
              for (int i = 0; i &lt; methods.length; i++) {
                  if (methods[i].getName().equals("methodName")) {
                      Log.w(TAG, "Method Found!");
                      methods[i].invoke("methodName", "string data", 0123, 123, "string data");
                  }
              } */
  
              //Second Attempt
              /*Class c=Class.forName(className);
              Method m=c.getMethod("methodName");
              Object t = c.newInstance();
              Object o= m.invoke(t,"string data", 0123, 123, "string data");*/
  
              //Third attempt
              /*Object carObj = Class.forName(className).newInstance();
              Method method = carObj.getClass().getMethod("methodName");
              method.invoke(method, "string data", 0123, 123, "string data"); */
  
          }catch(Exception e){
              Log.e("Invoke Error","Unable to invoke method =&gt; " + e.toString());
          }
      }
  }
  </code></pre>
  
  <p>I am currently stock and totally confused about invoke method. How can I solve this? I don't prefer the first attempt because it needs to list all the methods from a class just to check the specific method before invoking it.</p>