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 6470727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:08:37+00:00 2026-05-25T06:08:37+00:00

My problem is simple. So simple I can’t seem to figure it out. I

  • 0

My problem is simple. So simple I can’t seem to figure it out. I am passing a qualified class name to a BroadcastReceiver as a String in an Intent.

The goal is to instantiate an object from the class name using reflection, and call a method which we know exists, because it implements a proprietary interface. I am even checking it against getDeclaredMethods() which says its there, but when I try to invoke that method, it throws a java.lang.NoSuchMethodException.

String myClass = intent.getStringExtra("name");        
Class<?> c = Class.forName(myClass);
Object object = c.newInstance();

// Get the files from the interface

Method ms[] = c.getDeclaredMethods();
for (Method m1 : ms) {
    Log.i("METHOD", "METHODS " + m1.getName());
}

Method m = c.getDeclaredMethod("getStrings", (Class<?>) null);
String[] someStrings = (String[]) m.invoke(object, (Object[]) null);

m = c.getDeclaredMethod("getThings", (Class<?>) null);
Thing[] things = (Thing[]) m.invoke(object, (Object[]) null);

Here is the LogCat output:

08-30 23:37:36.150: INFO/METHOD(2336): METHODS getStrings
08-30 23:37:36.150: INFO/METHOD(2336): METHODS getThings
08-30 23:37:36.150: WARN/System.err(2336): java.lang.NoSuchMethodException: getStrings
08-30 23:37:36.150: WARN/System.err(2336):     at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
08-30 23:37:36.150: WARN/System.err(2336):     at java.lang.Class.getDeclaredMethod(Class.java:731)
08-30 23:37:36.150: WARN/System.err(2336):     at com.app.MyBroadcastReceiver.onReceive(MyBroadcastReceiver.java:45)
08-30 23:37:36.150: WARN/System.err(2336):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
08-30 23:37:36.150: WARN/System.err(2336):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
08-30 23:37:36.150: WARN/System.err(2336):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
08-30 23:37:36.150: WARN/System.err(2336):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 23:37:36.160: WARN/System.err(2336):     at android.os.Looper.loop(Looper.java:130)
08-30 23:37:36.160: WARN/System.err(2336):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-30 23:37:36.160: WARN/System.err(2336):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 23:37:36.160: WARN/System.err(2336):     at java.lang.reflect.Method.invoke(Method.java:507)
08-30 23:37:36.160: WARN/System.err(2336):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-30 23:37:36.160: WARN/System.err(2336):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-30 23:37:36.160: WARN/System.err(2336):     at dalvik.system.NativeStart.main(Native Method)

Is there something I am missing here?

Edit: Added the code to the class

public class MyClass implements MyInterface {

    @Override
    public String[] getStrings() {
        // Stub
        return new String[2];
    }

    @Override
    public Thing[] getThings() {
        // Stub
        return new Thing[2];
    }

}

Note: The BroadcastReceiver and the Class are in different projects.

  • 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-05-25T06:08:38+00:00Added an answer on May 25, 2026 at 6:08 am

    getDeclaredMethod requires you give the right parameters. If there are no parameters, then instead of passing “null”, you need to pass absolutely nothing.

    For example, here is a code snippet for hashCode, which takes no arguments

    Method m = c.getDeclaredMethod("hashCode");
    System.out.println("test");
    Method m2 = c.getDeclaredMethod("hashCode", (Class<?>) null);
    

    it will print out “test”, then throw a MethodNotFound exception as hashCode takes no arguments. If you want to get the argument types as well as the name, execute the getParameterTypes on the method object

    for (Method m1 : ms) {
        System.out.println( "METHODS " + m1.getName());
        for (Class paramC : m1.getParameterTypes()) {
            System.out.println(paramC.getName());
        }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a real simple problem, but can't seem to figure it out. The
Simple problem that I can't figure out... How can I print a '%' character
The problem itself is simple, but I can't figure out a solution that does
this is probably quite a simple problem, however, I can't figure out how to
This should be a really simple problem, but I can't quite figure out what
This is probably a really simple problem, but I can't figure out how to
This must be a simple problem but i can't figure out why it's not
I have a very simple problem, but I just can't figure it out. My
I'm just stuck with a problem (maybe Simple). But I can't figure out how
I've a simple problem with animation my button. I can't figure it out since

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.