[英]Invoke method inside a Thread
I just learn about Invoking Method from this link , and now I am trying to used it but I can't make it work with the logic I want. 我只是从此链接中了解了调用方法,现在我正在尝试使用它,但无法使其与所需的逻辑一起使用。 I simply want to invoke a method from a Thread.
我只是想从线程中调用方法。
I've used the below code and it work. 我使用了下面的代码,它可以工作。 But the problem here is the loop, The loop have to list all the method from a class and check if the specific method is exist and then invoke it.
但是这里的问题是循环,循环必须列出一个类中的所有方法,并检查特定方法是否存在,然后调用它。
Class myclass = Class.forName("com.package.MainActivity");
Method[] methods = myclass.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals("methodName")) {
Log.w(TAG, "Method Found!");
methods[i].invoke("methodName", "string data", 0123, 123, "string data");
}
}
I want to make it simple with the below code, but it keeps on telling me that the method was not found. 我想用下面的代码简化它,但是它一直告诉我找不到该方法。
ERROR: java.lang.NoSuchMethodException: methodName []
Class c=Class.forName("com.package.MainActivity");
Method m=c.getMethod("methodName");
Object t = c.newInstance();
Object o= m.invoke(t,"string data", 0123, 123, "string data");
I also tried the below code, but it's also not working and the error says : Can't create handler inside thread that has not called Looper.prepare()
我也尝试了下面的代码,但是它也无法正常工作,并且错误提示:
Can't create handler inside thread that has not called Looper.prepare()
Object carObj = Class.forName("com.package.MainActivity").newInstance();
Method method = carObj.getClass().getMethod("methodName");
method.invoke(method, "string data", 0123, 123, "string data");
I am doing all the above code with the below: 我正在用下面的代码做以上所有代码:
Main Class where the method should be call 应调用方法的主类
public class MainActivity extends AppCompatActivity {
static MyHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.btnStart);
myHelper = new MyHelper(this);
myHelper.StartThread();
}
/*The Method to Invoke*/
public static void methodName(String data1, int var1, int var2, String data2){
//some code here...
}
}
Helper Class for starting Thread including some of my functions 用于启动Thread的Helper类,包括我的一些功能
public class MyHelper{
private Context context;
public MyHelper(Context context){
this.context = context;
}
public static void StartThread(){
TheThread theThread = new TheThread(context);
Thread t = new Thread(theThread);
t.start();
}
//Some code below
}
The Thread where the method should be invoke 应该在其中调用方法的线程
public class TheThread implements Runnable{
private Context context;
public TheThread(Context context){
this.context = context;
}
@Override
public void run(){
try{
String className = context.getClass().getName(); //The Class Name including its package.
//First Attempt
/*Class myclass = Class.forName(className);
Method[] methods = myclass.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals("methodName")) {
Log.w(TAG, "Method Found!");
methods[i].invoke("methodName", "string data", 0123, 123, "string data");
}
} */
//Second Attempt
/*Class c=Class.forName(className);
Method m=c.getMethod("methodName");
Object t = c.newInstance();
Object o= m.invoke(t,"string data", 0123, 123, "string data");*/
//Third attempt
/*Object carObj = Class.forName(className).newInstance();
Method method = carObj.getClass().getMethod("methodName");
method.invoke(method, "string data", 0123, 123, "string data"); */
}catch(Exception e){
Log.e("Invoke Error","Unable to invoke method => " + e.toString());
}
}
}
I am currently stock and totally confused about invoke method. 我目前很库存,对调用方法完全感到困惑。 How can I solve this?
我该如何解决? I don't prefer the first attempt because it needs to list all the methods from a class just to check the specific method before invoking it.
我不喜欢第一次尝试,因为它需要列出一个类中的所有方法,只是为了在调用它之前检查特定的方法。
Your second example is almost Ok, except that you must tell it which parameters the method has. 第二个示例几乎是可以的,除了必须告诉它该方法具有哪些参数。
Class c=Class.forName("com.package.MainActivity");
Method m=c.getMethod("methodName", String.class, int.class, int.class, String.class);
Object t = c.newInstance();
Object o= m.invoke(t,"string data", 0123, 123, "string data");
Remember that the method signature is always composed of: methodName + list of parameter types. 请记住,方法签名始终由以下组成:methodName +参数类型列表。 If you don't tell the parameter types it will seek for a method which doesn't have any arguments.
如果您不告诉参数类型,它将搜索没有任何参数的方法。 That's why it throws the NoSuchMethod exception in your second example.
这就是为什么它在第二个示例中引发NoSuchMethod异常。
As a side note, if your method is static (as it is the case in the example) don't bother with the class instance. 附带说明一下,如果您的方法是静态的(如示例中的情况),则不必理会类实例。 You can pass a null as the first parameter of the m.invoke call.
您可以将null作为m.invoke调用的第一个参数传递。 See the Method javadoc: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...- .
请参见方法javadoc: https : //docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#invoke-java.lang.Object-java.lang.Object...- 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.