title: NoSuchMethod Exception while using reflection api in Android
body: <p>I have 2 classes in my android app. <code>Class1</code> and <code>Class2</code>. I have some methods in class2 which i want to invoke using reflection API. </p>
  
  <p>To invoke "printString" of class2, i am using following code:</p>
  
  <pre><code>public void printString(String str) {
          Log.d("TAG", "in printString" + str);
      }
  </code></pre>
  
  <p>Reflection Code:</p>
  
  <pre><code>Method method = Class2.class.getMethod("printString");
  method.invoke("asjdhaskdf", null);
  </code></pre>
  
  <p>But using this code is throwing following exception:</p>
  
  <pre><code>10-26 16:10:16.931: W/System.err(25036): java.lang.NoSuchMethodException: printString []
  10-26 16:10:16.931: W/System.err(25036):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
  10-26 16:10:16.931: W/System.err(25036):    at java.lang.Class.getMethod(Class.java:915)
  10-26 16:10:16.931: W/System.err(25036):    at com.sample.mobile.android.ui.activities.Class1Activity$1.onClick(Class1Activity.java:56)
  10-26 16:10:16.931: W/System.err(25036):    at android.view.View.performClick(View.java:3627)
  10-26 16:10:16.931: W/System.err(25036):    at android.view.View$PerformClick.run(View.java:14329)
  10-26 16:10:16.936: W/System.err(25036):    at android.os.Handler.handleCallback(Handler.java:605)
  10-26 16:10:16.936: W/System.err(25036):    at android.os.Handler.dispatchMessage(Handler.java:92)
  10-26 16:10:16.936: W/System.err(25036):    at android.os.Looper.loop(Looper.java:137)
  10-26 16:10:16.941: W/System.err(25036):    at android.app.ActivityThread.main(ActivityThread.java:4511)
  10-26 16:10:16.941: W/System.err(25036):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-26 16:10:16.941: W/System.err(25036):    at java.lang.reflect.Method.invoke(Method.java:511)
  10-26 16:10:16.941: W/System.err(25036):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:986)
  10-26 16:10:16.941: W/System.err(25036):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
  10-26 16:10:16.941: W/System.err(25036):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Please advise what i am doing wrong. I have read tutorials available on internet but nothing worked.</p>
  
  <p><strong>Update 1</strong></p>
  
  <p><em><strong>One Imp Point: My code never reached on "method.invoke" line. It throws an exception on the line where i am getting the method using "getMethod" function.</em></strong></p>
  
  <p>Code of Class2:</p>
  
  <pre><code>package com.sample.mobile.android.utils;
  
  public class Class2{
  
      public void printString(String str) {
         Log.d("TAG", "in printString" + str);
      }
  
  }
  </code></pre>
  
  <p>Code of class1Activity:</p>
  
  <pre><code>package com.sample.mobile.android.ui.activities;
  
  public class Class1Activity extends Activity {
  
      private Class2 obj; 
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          obj = new Class2();
          Class cls;
          try {
              cls = Class
                      .forName("com.sample.mobile.android.utils.Class2");
  
              Method[] methods = cls.getMethods();
              Log.d("TAG", "length -- " + methods.length);
  
              for (int i = 0; i &lt; methods.length; i++) {
                  Log.d("TAG", "name -- " + methods[i].getName());
                  // this is printing the name of all  methods including "printString".
              }
  
              Method method = cls.getMethod("printString");
              method.invoke(obj, "asjdhaskdf");
  
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IllegalArgumentException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IllegalAccessException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (InvocationTargetException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (SecurityException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (NoSuchMethodException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>