No Such Method Exception - using Reflection - android

I'm trying to use reflection (on an android app) to invoke a method and it work only when I do it this way
Object impresora = loadedClass.newInstance();
Object args[] = {"00:15:0E:E0:DD:38", true};
for(Method m : impresora.getClass().getDeclaredMethods())
if("BTConnection".compareTo(m.getName()) == 0)
int resultado = (Integer) m.invoke(impresora, args);
But I don't want to iterate everytime, so I'm trying this way, but this is where I get the NoSuchMethodException
Method m = impresora.getClass().getDeclaredMethod("BTConnection");
m.invoke(impresora, args);
Thanks in advance

In your first snippet you're doing object.getClass() in your second snippet you're doing impresora.getClass().

You need the actual parameter types in order to find the methods otherwise it will try to look for the method without an argument which I am guessing doesn't exist in your class.
Seeing:
Object args[] = {"00:15:0E:E0:DD:38", true};
I am guessing that the first argument is a String and second one is a boolean, so in order to find the method you need to do the following:
Method m = c.getDeclaredMethod("BTConnection", String.class, Boolean.class);

Related

How can I get the value in android sugar, I'm only getting the object reference

I'm using for first time sugar and all seems that work fine. I can save the data and seems that I'm getting the data when I try to do a find. My problem is that I'm getting the object and not the value that I have store and I'm not really sure why, because I'm doing the same that I can see in the official documentation
This is that I'm doing to get the data:
Select name= Select.from(MyClass.class).where(Condition.prop("name").lt("Bob"));
String data = name.toString();
Log.e("aaaaa", data.toString());
This is that I'm gettin in the log:
com.orm.query.Select#3ce7ff7b
With that Select statement, you are querying for the MyClass object by name, and that returns you a Select object.
To get the name from it you should fetch results from the Select (here I do it with .first()) and put that in a MyClass object. Then you can get the name from that.
MyClass myClass = Select.from(MyClass.class)
.where(Condition.prop("name").lt("Bob"))
.first();
String name = myClass.getName();
provided your MyClass has a method getName()
I'm doing the same that I can see in the official documentation
I don't thnk you are.
In the documentation here, it shows this piece of code:
Select.from(TestRecord.class)
.where(Condition.prop("test").eq("satya"),
Condition.prop("prop").eq(2))
.list();
When you compare it with your code, you see that you missed the call to list.
If you don't call list, the where method will return a Select object. And that's all you get.
list will return all of the results that the query has found. So the correct way to do this would be:
List<MyClass> results =
Select.from(MyClass.class).
where(Condition.prop("name").
lt("Bob")).list();
for (MyClass obj: results) {
Log.i("aaaa", obj.toString());
// or you can write
// Log.i("aaaa", obj.name);
}
The above would print all the results. If you only need the first result, you can call results.get(0).

Android Parse finding a result using a Pointer Id

I'm trying to get a query using as condition the Pointer Id of the object, for example I'm saving all the id's of an object in an array, then I want to get from another class (related by pointer), all the objects that uses that Id, so I already have this:
for (i = 0; i < num; i++) {
//restaurant.setObjectId(restId[i]);
ParseQuery<ParseObject> resultsitems = ParseQuery.getQuery("Item").whereEqualTo ("restaurant", restId[i]);
try {
objects=resultsitems.find();
} catch (ParseException e) {
e.printStackTrace();
}
(.......)
}
In my first try I tried to set the id into the restaurant object, then tried to use the query as:
ParseQuery<ParseObject> resultsitems = ParseQuery.getQuery("Item").whereEqualTo ("restaurant",restaurant );
But it didn't work, then I tried to search as shown in the code above, it doesn't crash but brings me nothing, how can I do this?
This is what really worked for me:
ParseObject obj = ParseObject.creatWithoutData("classNameThatPointedTo","fieldValue");
query.whereEqualTo("fieldName", obj);
Use: .findInBackground(new FindCallBack<Item>... (this will auto-complete in Android Studio), then put objects=resultsitems.find(); in the curly braces of the done() function.
The callback waits for the query to return before moving on with the script. Otherwise, the main thread will keep moving on without waiting for the data to come back from the server.

Can't readout ArrayList in Android

I deliver a ArrayList to another method, where I just wanna readout a specific String of the list.
public void pruefeWerHat(ArrayList<Teilnehmer> test){
System.out.println(test);
I get this in LogCat
"1 PeterPan 0 0, 2 Hansi 0 0"
now I just want to use the name, but if I say (after sysout)
String name = test.get(1);
the problem he said to is, that he cannot convert from Teilnehmer to String. I also tested Teilnehmer.get(1) but it doesn't work neither.
When you do
System.out.println(test);
the toString() method is automatically used. This method is in the Object class, so all objects in java can call this method.
When you do
String name = test.get(1);
the toString() method is not called on it's own, you have to call it yourself. To do this, simply use
String name = test.get(1).toString();
Also, if you want to change what is printed, you can overwrite the toString() method in your class.
#Overwrite
public String toString() {
String stringToPrint = "This string will be printed";
return stringToPrint;
}
Now when you do
System.out.println(test);
instead of seeing "1 PeterPan 0 0, 2 Hansi 0 0" you will see "This string will be printed" (or whatever you choose to add in your toString() implementation.
When you print test toString function is called so use this in your code
String name = test.get(1).toString();
What are the members of Teilnehmer?
You need to use something like
string name = (Teilnehmer)test[1].Name
where Name is the field you are trying to extract
The get(int index) method available to ArrayList returns type E. In this instance, it returns type Teilnehmer, which is obviously not a String. You can try and cast Teilnehmer (although probably not desirable) to String or simply call the .toString() method (e.g; test.get(1).toString()) inherited from type Object. Or, if desired, calling a method that returns a String. test.get(1).getNameAsString();
The reason you are allowed to call the type in System.out.println(Teilnehmer) is that println makes a call to the object's string representation:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}

Java Class Method Error android

I have a method inside of a standard Java class that takes in a String[] as a parameter and returns a String[]. Basically the method is reading the Shared Preferences and returning a String[]. Here it is:
public static String[] getPrefs(){
String tempString = settings.getString("123", "0");
if(tempString == ("0")){
//show some type of error
return null;
}
String[] ToReturn = tempString.split("#,#");
return ToReturn;
And the Error:
Here is a link to a picture with my error at line 3.
I also got an error that didn't say much other than that it was at line 4.
Before you ask, the LogCat didn't give any more info than what I just list. These are all runtime errors and eclipse doesn't detect any errors.
Thanks in advance for any help.
Change the line to
if (tempString == null) {
//show some type of error
return null;
}
This is how you check for null, not by calling .equals(). Calling .equals() (or any method) on a null object will cause a NullPointerException.
Edit: After your third edit, you need to change it to
if(tempString.equals("0")){
Since tempString will no longer default to null, as it did prior to your edit.

Android: Using a string in place of an object/instance name

I would like to see if I can avoid a lengthy switch or if block by directly converting some strings into an object name. For example, I have a class called Example and I want to [edit] have up to 10 instances of the class Example1, Example2, so on. Can I use something like:
int ExampleNum = 2;
// can be changed to any 1-10 value corresponding to instances
String s = "Example" + String.valueOf(ExampleNum);
Refresh(s);
public void Refresh(Example example){
...
}
Thus I would create a string with the value of Example2 and pass that to my Refresh method.
[edit]
I don't want to use all the instances at once, but rather have other methods that change the int ExampleNum so that when I try to refresh it refreshes the appropriate Example instance.
Rather than saying:
if (ExampleNum == 2)
Refresh(Example2);
I would use the ExampleNum and String to use the right instance name;
Why not use array's instead??
Example[] e = null;
for(int i=1;i<=10;i++)
{
e[i] = new Example();
Refresh(e[i]);
}
Well, your code, as it stands now, doesn't make any sense since you're passing a String to Refresh, which takes an Example object as an argument.
However, if you're asking how you can create the strings Example1, Example2, ... Example 10, you can do this:
for (int i = 1; i <= 10; i++) {
s = "Example" + i;
refresh(s); // assuming this takes a string
}

Categories

Resources