Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:56:17+00:00 2026-06-13T09:56:17+00:00

I have 2 classes in my android app. Class1 and Class2 . I have

  • 0

I have 2 classes in my android app. Class1 and Class2. I have some methods in class2 which i want to invoke using reflection API.

To invoke “printString” of class2, i am using following code:

public void printString(String str) {
        Log.d("TAG", "in printString" + str);
    }

Reflection Code:

Method method = Class2.class.getMethod("printString");
method.invoke("asjdhaskdf", null);

But using this code is throwing following exception:

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)

Please advise what i am doing wrong. I have read tutorials available on internet but nothing worked.

Update 1

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.

Code of Class2:

package com.sample.mobile.android.utils;

public class Class2{

    public void printString(String str) {
       Log.d("TAG", "in printString" + str);
    }

}

Code of class1Activity:

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 < 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();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:56:18+00:00Added an answer on June 13, 2026 at 9:56 am

    it because there is no such method

    you’re trying invoke this method on String class

    it should be method.invoke(instanceofClass2, "asjdhaskdf")

    EDIT: to match Update 1

    … and since printString has String parameters you should use getMethod like this:

    Method method = Class2.class.getMethod("printString", String.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app in android a have many classes and I want to order
I have a class A in my Android app where I have some methods.
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
i have an android project which has a lot of classes I've built. i
I have developed some reusable android component which is basically a class . This
I have an Android app which uses Jackson parser for JSON parsing. After I've
Suppose that, I have an android app that launches browser with some url supplied
I already have a working android app with several persisted objects/classes. But what if
I have splitted my application into several packages . Some of my classes which
I want to use some DevicePolicyManager methods in my app. The DevicePolicyManager was introduced

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.