Invoke getMessageWaitingIndicator count of PhoneBase.java - java

I am trying to retrieve count of voicemail .For this I am trying to call getMessageWaitingIndicator method of PhoneBase.java.
My Code is as given below
Class<?> class2=Class.forName("com.android.internal.telephony.PhoneBase");
Method method=class2.getMethod("getMessageWaitingIndicator");
Boolean returnValue=(Boolean) method.invoke(class2);
But Everytime I am getting an exception as mentioned below :
java.lang.IllegalArgumentException: expected receiver of type com.android.internal.telephony.PhoneBase,
but got java.lang.class<com.android.internal.telephony.PhoneBase>
I don't understand it. How can I fix this issue?

The instruction:
method.invoke (stringValue);
needs the object in which the method will be invoked.
method = class2.getMethod('myFunction',String.class);
method.invoke(someInstanceOfMyActivity, stringValue);
Documentation:
http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html
Edit.
You can also look at the answer to the very close question here:
Android Reflection Method Error

Related

Flurry Integration Error: Cannot find symbol method init()

In my application class, in OnCreate(), I've added the following bit of code:
new FlurryAgent.Builder()
.withDataSaleOptOut(false) //CCPA - the default value is false
.withCaptureUncaughtExceptions(true)
.withIncludeBackgroundSessionsInMetrics(true)
.withLogLevel(Log.VERBOSE)
.withPerformanceMetrics(FlurryPerformance.ALL)
.build(context, API_KEY);
Also, in the gradle, I've added implementation like,
implementation 'com.flurry.android:analytics:12.11.0'
Now, when I'm trying to add FlurryAgent.init(Context, String) in a method, I'm receiving the error "cannot find symbol method init(Context, String). The same is the case with FlurryAgent.setLogEnabled(True).
This is the first time I'm working with Flurry. Can anyone please guide me?
FlurryAgent.init(Context, String) method was removed from Flurry SDK. You should use FlurryAgent.Builder()...build(Context, String) to initialize Flurry.
Looks like you already use FlurryAgent.Builder.build here. You don't need to use FlurryAgent.init again.

How to mock the second object in the same method?

Now I have a method to test using Mockito . However the method is kind of complicated. In the same method ,I need to new two objects both are the same type.
Timestamp beginTimestamp = new Timestamp(Long.parseLong(beginTimeLong));
Timestamp endTimestamp = new Timestamp(System.currentTimeMillis());
I want to mock the second object , endTimestamp , to throw an Exception , but I can not avoid the influence of beginTimestamp. Now my question is how to mock the second object, endTimeStamp, only ,and make it to throw out an exception when I call endTimestamp's certain method ,such as:
endTimestamp.getTime()
I tried to write my test code which is shown below ,
#Ignore
public void getSynPotentialShopBeginTimeAndEndTest4() throws Exception {
Timestamp beginTimestamp = PowerMockito.mock(Timestamp.class);
Timestamp endTimestamp = PowerMockito.mock(Timestamp.class);
PowerMockito.whenNew(Timestamp.class).withAnyArguments().thenReturn(beginTimestamp).thenReturn(endTimestamp);
when(endTimestamp.getTime()).thenThrow(RuntimeException.class);
redisService.getSynPotentialShopBeginTimeAndEnd();
}
It doesn't work either. These code don't have any red wavy underline ,but when I tried to run it, I got an exception like this:
org.mockito.exceptions.base.MockitoException:
Incorrect use of API detected here:
You probably stored a reference to `OngoingStubbing` returned by `when()` and called stubbing methods like `thenReturn()` on this reference more than once.
Examples of correct usage:
when(mock.isOk()).thenReturn(true).thenReturn(false).thenThrow(exception);
when(mock.isOk()).thenReturn(true, false).thenThrow(exception);
Is there another solution I can solve the problem? Anyway , only if the problem to be solved ,that's OK.
Try this
PowerMockito.whenNew(Timestamp.class).withAnyArguments().thenReturn(beginTimestamp,endTimestamp);
Instead of PowerMockito.whenNew(Timestamp.class).withAnyArguments().thenReturn(beginTimestamp).thenReturn(endTimestamp);

SetDataAndType method

As documented in Android Developers page setDataAndType method can be called in this way:
auto setDataAndTypeFunctionId = env->GetMethodID(intentClassId, "setDataAndType", "(Landroid/net/Uri;Ljava/lang/String;)V");
jstring jmime = env->NewStringUTF("video/*");
env->CallVoidMethod(context, setDataAndTypeFunctionId, intentObject, UriObject, jmime);
But when i run my app this error show on output:
java.lang.NoSuchMethodError: no method with name='setDataAndType' signature='(Landroid/net/Uri;Ljava/lang/String;)V' in class Landroid/content/Intent;
The problem is the return type, which is not void. The setDataAndType method returns an object of type Intent.
Use the following signature:
env->GetMethodID(intentClassId, "setDataAndType", "(Landroid/net/Uri;Ljava/lang/String;)Landroid/content/Intent;");
Also, you should use CallObjectMethod instead of CallVoidMethod, although it doesn't really matter because you're not using the return value.

Integer resource not returning the correct value

In my integer.xml I have
<integer name="minUNameLen">6</integer>
And in my code I am :
if (uName.trim().length() < R.integer.minUNameLen) {
Toast.makeText(
Splash.this.getApplicationContext(),
"Should be Min "+R.integer.minUNameLen+" characters long",Toast.LENGTH_LONG).show();
But instead of returning 6 I am getting 2131165…. a weird number in my code.
Can anybody tell what’s wrong in here?
Doing Resources.getInteger(R.integer.minUNameLen) from here gives me Cannot make a static reference to the non-static method getInteger(int) from the type Resources
That's the resource ID you're seeing.
You need to use
getResources().getInteger(R.integer.minUNameLen)
Use getResources().getInteger(R.integer.minUNameLen) to get the value
What you are getting is the value of the variable R.integer.minUNameLen
What you need is: Activity.getResources().getInteger(R.integer.minUNameLen)
You need a Context object instance to retrieve a Resources object instance and finally get the desired resource, an Integer in your case.
myActivity.getContext().getResources().getInteger(R.integer.yourIntegerID);
as Activity extends Context you can simply call
myActivity.getResources().getInteger(R.integer.yourIntegerID);

com.sun.jdi.InvocationException occurred invoking method error in ognlRuntime

I am getting "com.sun.jdi.InvocationException occurred invoking method" error when I debug in eclipse and is in ognlRuntime.
The error occured when I am passing a value from jsp to the action class.
This is the code in jsp for passing the value
<s:textfield name="partReplace.part.number" cssStyle="width:50px;" value ="%{partReplace.part.number}"/></td>
and when I enter vlue in that textfield it is not setting in the action class. I debuuged and found the "com.sun.jdi.InvocationException occurred invoking method" error in ognlRuntime and it is not taking the value..
Your problem seems to be the same of this question;
if so, the guilty should be an incorrect override of the toString() method of your class, invoked by the Eclipse Debugger.
Correct your custom toString() method, or block him from throwing exceptions (a big try-catch inside) or remove it completely, and retry.
P.S: this is the first result on Google searching your Exception... and is on SO too :|

Categories