Java Question Getting the class of a activity.

barx

Well-Known Member
Licensed User
Longtime 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

B4X:
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
 

barx

Well-Known Member
Licensed User
Longtime 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

B4X:
nb.setParentActivity(main2)

then in lib I have

B4X:
stackBuilder.addParentStack(pActivity.getClass());

After I compile the b4a test project I get an error in the log

B4X:
** 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

B4X:
return pActivity.getClass().toString();

And all it ever seems to return is 'java.lang.Class'

Puzzled!
 
Last edited:

barx

Well-Known Member
Licensed User
Longtime User
Just tested for curiosity sake with

B4X:
stackBuilder.addParentStack(ba.activity.getClass());

And get a NullPointerException

B4X:
** 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
 

barx

Well-Known Member
Licensed User
Longtime User
The confusing thing is the Object is checked for Null before hand this is the code used.

B4X:
   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.
 

barx

Well-Known Member
Licensed User
Longtime 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.
 

barx

Well-Known Member
Licensed User
Longtime User
Just run the code and this line

B4X:
stackBuilder.addParentStack(pActivity.getClass());

Causes this error in log

B4X:
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

B4X:
BA.Log(pActivity.getClass().getName());

Thanks
 

agraham

Expert
Licensed User
Longtime User
This is a wild guess and you may have tried this already! The Android docs for TaskStackBuilder.addParentStack(Class<?> sourceActivityClass) say
Add the activity parent chain as specified by manifest <meta-data> elements to the task stack builder.
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.
 

Informatix

Expert
Licensed User
Longtime 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):

B4X:
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)));
}
 

barx

Well-Known Member
Licensed User
Longtime User

I foreseen this as a possible issues, hence this post

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

Thanks for the pointer though agraham, it all helps
 

barx

Well-Known Member
Licensed User
Longtime User

Will give it a try Informatix and report back thanks.
 

barx

Well-Known Member
Licensed User
Longtime 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

B4X:
ComponentInfo{barxdroid.nbtest/barxdroid.nbtest.main2}

So I tried tweaking the code to

B4X:
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.

B4X:
** 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)
 

barx

Well-Known Member
Licensed User
Longtime User
That's expected behaviour. Try getClass().getName() to see the actual class.

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

B4X:
stackBuilder.addParentStack(resultActivity.class);

not

B4X:
stackBuilder.addParentStack("resultActivity.class");

Any other ideas guys, tried loads of things, just not the right one lol
 

Informatix

Expert
Licensed User
Longtime User
Reporting back as promised.

Added the code you posted Informatix and got an NoSucgMethodError.

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.
 

barx

Well-Known Member
Licensed User
Longtime 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
 

Informatix

Expert
Licensed User
Longtime 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 ?
 

barx

Well-Known Member
Licensed User
Longtime User
1) yes I have

B4X:
@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:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…