Java Question Getting the class of a activity.

Discussion in 'Libraries developers questions' started by barx, Mar 6, 2013.

  1. barx

    barx Well-Known Member Licensed User

    Trying to build a Notification Builder lib and have come across a little problem.

    When trying to create a false backstack, you use a line something like

    Code:
    stackBuilder.addParentStack(ResultActivity.class);
    As you can see a class is passed as arg0

    How can I pass a b4a activity object and get it's class for this line?

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Something like:
    Code:
    public void YourMethod(BA ba, other parameters...) {
     stackBuilder.addParentStack(ba.activity.getClass());
    }
     
  3. barx

    barx Well-Known Member Licensed User

    I tried something similar and get an error.

    Basically i have a method called setParentActivity and pass a b4a activity as an Object and assign it it to an Object called pActivity

    Code:
    nb.setParentActivity(main2)
    then in lib I have

    Code:
    stackBuilder.addParentStack(pActivity.getClass());
    After I compile the b4a test project I get an error in the log

    Code:
    ** Activity (main) Create, isFirst = true **


    Class returned class java.lang.Class


    main_activity_create (java line: 
    248)
    java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{barxdroid.nbtest/java.lang.Class
    }


       at android.support.v4.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:197)
       at barxdroid.NotificationBuilder.NotificationBuilder.Notify(NotificationBuilder.java:116)
       at barxdroid.nbtest.main._activity_create(main.java:248)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
       at barxdroid.nbtest.main.afterFirstLayout(main.java:85)
       at barxdroid.nbtest.main.access$100(main.java:16)
       at barxdroid.nbtest.main$WaitForLayout.run(main.java:72)
       at android.os.Handler.handleCallback(Handler.java:605)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4575)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
       at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{barxdroid.nbtest/java.lang.Class}
       at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:223)
       at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:264)
       at android.support.v4.app.NavUtils.getParentActivityIntent(NavUtils.java:211)
       at android.support.v4.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:190)
       ... 17 more
    java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{barxdroid.nbtest/java.lang.Class}
    I have also tried creating a testing method to that does

    Code:
    return pActivity.getClass().toString();
    And all it ever seems to return is 'java.lang.Class'

    Puzzled!
     
    Last edited: Mar 7, 2013
  4. barx

    barx Well-Known Member Licensed User

    Just tested for curiosity sake with

    Code:
    stackBuilder.addParentStack(ba.activity.getClass());
    And get a NullPointerException

    Code:
    ** Activity (main) Create, isFirst = true **


    main_activity_create (java line: 
    246)


    java.lang.NullPointerException
       at barxdroid.NotificationBuilder.NotificationBuilder.Notify(NotificationBuilder.java:
    116)
       at barxdroid.nbtest.main._activity_create(main.java:
    246)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:
    511)
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    167)
       at barxdroid.nbtest.main.afterFirstLayout(main.java:
    85)
       at barxdroid.nbtest.main.access$
    100(main.java:16)
       at barxdroid.nbtest.main$WaitForLayout.run(main.java:
    72)
       at android.os.Handler.handleCallback(Handler.java:
    605)
       at android.os.Handler.dispatchMessage(Handler.java:
    92)
       at android.os.Looper.loop(Looper.java:
    137)
       at android.app.ActivityThread.main(ActivityThread.java:
    4575)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:
    511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    556)
       at dalvik.system.NativeStart.main(Native Method)
    java.lang.NullPointerException
     
  5. barx

    barx Well-Known Member Licensed User

    The confusing thing is the Object is checked for Null before hand this is the code used.

    Code:
    if(nActivity != null) {
             
    Intent resultIntent = new Intent(Common.getComponentIntent(ba, nActivity));
             
    if(pActivity != null) {
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(ba.context);
                stackBuilder.addParentStack(pActivity.getClass());
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
    0, PendingIntent.FLAG_UPDATE_CURRENT);
                ((Builder)getObject()).setContentIntent(resultPendingIntent);
             
    }
          }
    So I guess the Object exists but the getClass() isn't doing what is required.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    The problem is not in getClass().
     
  7. agraham

    agraham Expert Licensed User

    That's expected behaviour. Try getClass().getName() to see the actual class.
     
  8. barx

    barx Well-Known Member Licensed User

    Indeed the NullPointException wasn't to do with the getClass().

    But now I get a IllegalState error. Will get the exact error later when I am back home.
     
  9. barx

    barx Well-Known Member Licensed User

    Just run the code and this line

    Code:
    stackBuilder.addParentStack(pActivity.getClass());
    Causes this error in log

    Code:
    LogCat connected to: SH0BCRT05610
    --------- beginning of /dev/
    log/system


    --------- beginning of /dev/
    log/main


    ** 
    Activity (main) Create, isFirst = true **


    java.lang.Class
    main_activity_create (java line: 
    250)
    java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{barxdroid.nbtest/java.lang.Class
    }
       at android.support.v4.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:197)
       at barxdroid.NotificationBuilder.NotificationBuilder.Notify(NotificationBuilder.java:195)
       at barxdroid.nbtest.main._activity_create(main.java:250)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
       at barxdroid.nbtest.main.afterFirstLayout(main.java:85)
       at barxdroid.nbtest.main.access$100(main.java:16)
       at barxdroid.nbtest.main$WaitForLayout.run(main.java:72)
       at android.os.Handler.handleCallback(Handler.java:605)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4575)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
       at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{barxdroid.nbtest/java.lang.Class}
       at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:223)
       at android.support.v4.app.NavUtils.getParentActivityName(NavUtils.java:264)
       at android.support.v4.app.NavUtils.getParentActivityIntent(NavUtils.java:211)
       at android.support.v4.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:190)
       ... 17 more
    java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{barxdroid.nbtest/java.lang.Class}
    Any Ideas?

    the 'java.lang.Class in the log just before the Main_Activity_Create is what is returned from

    Code:
    BA.Log(pActivity.getClass().getName());
    Thanks
     
  10. agraham

    agraham Expert Licensed User

    This is a wild guess and you may have tried this already! The Android docs for TaskStackBuilder.addParentStack(Class<?> sourceActivityClass) say
    It's typically obtuse (I find the Android documentation is usually totally unhelpful) but the standard Basic4android manifest doesn't seem to have a <meta-data> tag so maybe it's complaining about the lack of a name attribute in the meta-data.
     
  11. Informatix

    Informatix Expert Licensed User

    Here's something you can try. I get no error with this code (ActModName is the activity module name as it appears in B4A, e.g. "Main" for the main activity):

    Code:
    public void Test(BA ba, String ActModName)
    {
       
    String PN = BA.packageName;
       TaskStackBuilder stackBuilder = TaskStackBuilder.create(ba.context);
       stackBuilder.addParentStack(new ComponentName(PN, PN + 
    "." + ActModName.toLowerCase(BA.cul)));
    }
     
  12. barx

    barx Well-Known Member Licensed User

    I foreseen this as a possible issues, hence this post

    http://www.basic4ppc.com/forum/basic4android-updates-questions/27094-manifest-editor-add-meta-data.html

    Thanks for the pointer though agraham, it all helps ;)
     
  13. barx

    barx Well-Known Member Licensed User

    Will give it a try Informatix and report back thanks.
     
  14. barx

    barx Well-Known Member Licensed User

    Reporting back as promised.

    Added the code you posted Informatix and got an NoSucgMethodError.

    Logged the .tostring of the ComponentName and it was returning

    Code:
    ComponentInfo{barxdroid.nbtest/barxdroid.nbtest.main2}
    So I tried tweaking the code to

    Code:
    ComponentName cn = new ComponentName(pn, "." + pActivity.toLowerCase((BA.cul)));
    Sill get the same error but as you can see from the log, the name now looks right.

    Code:
    ** Activity (main) Create, isFirst = true **


    -
    3


    java.lang.String


    ComponentInfo{barxdroid.nbtest/.main2
    }
    main_activity_create (java line: 250)
    java.lang.NoSuchMethodError: android.support.v4.app.TaskStackBuilder.addParentStack


       at barxdroid.NotificationBuilder.NotificationBuilder.Notify(NotificationBuilder.java:201)
       at barxdroid.nbtest.main._activity_create(main.java:250)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
       at barxdroid.nbtest.main.afterFirstLayout(main.java:85)
       at barxdroid.nbtest.main.access$100(main.java:16)
       at barxdroid.nbtest.main$WaitForLayout.run(main.java:72)
       at android.os.Handler.handleCallback(Handler.java:605)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4575)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
       at dalvik.system.NativeStart.main(Native Method)
     
  15. barx

    barx Well-Known Member Licensed User

    Cannot use .getName() for this as it returns a string. Needs to be

    Code:
    stackBuilder.addParentStack(resultActivity.class);
    not

    Code:
    stackBuilder.addParentStack("resultActivity.class");
    Any other ideas guys, tried loads of things, just not the right one lol
     
  16. agraham

    agraham Expert Licensed User

    I didn't think you could use it, I assumed that you just wanted a way to confirm that the object that you were trying to use was of the correct Class.
     
  17. Informatix

    Informatix Expert Licensed User

    You have a problem with the support library. "NoSuchMethodError" means the library does not contain a method "addParentStack", but the documentation and my own test prove that it is a method of TaskStackBuilder, without any doubt, and it is included in the support library. So you have a problem with your library. You should get the latest version.

    No, it looks wrong. The right name is the one you got first.
     
  18. barx

    barx Well-Known Member Licensed User

    Are you saying there is a problem with the support library file I am using?

    it is the one included in latest SDK. The addParentStack method comes up (and was selected) from the auto-complete in eclipse. So, you saying I should try and download another copy from somewhere?

    Thanks
     
  19. Informatix

    Informatix Expert Licensed User

    1) Did you add DependsOn in your Java code for the support lib ?
    2) What version do you have in your B4A libraries folder ?
     
  20. barx

    barx Well-Known Member Licensed User

    1) yes I have

    Code:
    @DependsOn(values = {"android-support-v4"})
    2) I don't any version in my b4a libs folder.

    edit: well at least I didn't think I did until I just tried to copy it over...
     
    Last edited: Mar 9, 2013
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice