Java教程

Caused by: java.lang.reflect.InvocationTargetException反射目标初始化异常

本文主要是介绍Caused by: java.lang.reflect.InvocationTargetException反射目标初始化异常,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

首先来看异描述信息:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.localhostservice, PID: 10788
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
at android.view.View.performClick(View.java:7147)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7120)
at android.view.View.access$3500(View.java:804)
at android.view.View$PerformClick.run(View.java:27538)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7399)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
at android.view.View.performClick(View.java:7147) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
at android.view.View.performClickInternal(View.java:7120) 
at android.view.View.access$3500(View.java:804) 
at android.view.View$PerformClick.run(View.java:27538) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7399) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) 
Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.example.roveservice }
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1559)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1727)
at android.app.ContextImpl.bindService(ContextImpl.java:1651)
at android.content.ContextWrapper.bindService(ContextWrapper.java:705)
at android.content.ContextWrapper.bindService(ContextWrapper.java:705)
at com.example.localhostservice.MainActivity.bingService(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
at android.view.View.performClick(View.java:7147) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
at android.view.View.performClickInternal(View.java:7120) 
at android.view.View.access$3500(View.java:804) 
at android.view.View$PerformClick.run(View.java:27538) 
at android.os.Handler.handleCallback(Handler.java:883) 
at android.os.Handler.dispatchMessage(Handler.java:100) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7399) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980) 

相信你们可能和我一样都是一脸的懵逼,,说真我也不知道具体的是什么?出现这样的异常因为反射目标没有指定,对于API越高的,对于用户的安全要求和机制越来越严格,如果不知道某一反射的包名,机会出现异常。

下面说说解决的方案:只需要在代码中加入这一行代码: 

intent.setPackage("指定的包名");
例如:
public void bingService(View view){
    Log.i(TAG,"触发了绑定服务的方法");
    Intent intent = new Intent("com.example.roveservice");
    //intent.setPackage("com.example.roveservice");
    conn = new MyConn();
    bindService(intent,conn,BIND_AUTO_CREATE);
}
轻松解决此异常。
这篇关于Caused by: java.lang.reflect.InvocationTargetException反射目标初始化异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!