Android - java.lang.reflect.InvocationTargetException调用dialog.show()

时间:2016-09-26 19:47:24

标签: android google-play-services android-dialog

我在Nexus 5仿真器(API 24 / Google API)上尝试运行我的代码(下面)时遇到上述异常......

E/ACRA: ACRA caught a RuntimeException for com.example
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.SplashActivity}: android.view.InflateException: Binary XML file line #25: Binary XML file line #44: Error inflating class com.android.internal.widget.DialogTitle
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
            at android.app.ActivityThread.-wrap12(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6077)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
         Caused by: android.view.InflateException: Binary XML file line #25: Binary XML file line #44: Error inflating class com.android.internal.widget.DialogTitle
         Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class com.android.internal.widget.DialogTitle
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
            at android.view.LayoutInflater.createView(LayoutInflater.java:645)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
            at android.view.LayoutInflater.parseInclude(LayoutInflater.java:994)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
            at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:414)
            at com.android.internal.app.AlertController.installContent(AlertController.java:236)
            at android.app.AlertDialog.onCreate(AlertDialog.java:423)
            at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
            at android.app.Dialog.show(Dialog.java:293)
            at com.example.SplashActivity.checkPlayServices(SplashActivity.java:195)
            at com.example.SplashActivity.onCreate(SplashActivity.java:74)
            at android.app.Activity.performCreate(Activity.java:6664)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
            at android.app.ActivityThread.-wrap12(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6077)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
         Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}
            at android.content.res.TypedArray.getColorStateList(TypedArray.java:528)
            at android.widget.TextView.<init>(TextView.java:783)
            at android.widget.TextView.<init>(TextView.java:704)
            at android.widget.TextView.<init>(TextView.java:700)
E/ACRA:     at com.android.internal.widget.DialogTitle.<init>(DialogTitle.java:41)
                ... 34 more

我看到类似问题的答案,建议将SplashActivity.this传递给Dialog构造函数而不是传递getApplicationContext() - 但我已经传递了一个活动实例。

任何想法可能出错?

我的代码!...

private boolean checkPlayServices() {
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    int result = googleAPI.isGooglePlayServicesAvailable(this);
    if (result != ConnectionResult.SUCCESS) {
    if (googleAPI.isUserResolvableError(result)) {
        Dialog dialog = googleAPI.getErrorDialog(this, result, PLAY_SERVICES_RESOLUTION_REQUEST);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            SplashActivity.this.finish();
        }
        });
        dialog.show();
    }
    return false;
    }
    return true;
}

2 个答案:

答案 0 :(得分:0)

下面的错误方式告诉你

java.lang.UnsupportedOperationException: 
   Failed to resolve attribute at index 6: TypedValue{t=0x2/d=0x101009b a=1}

这似乎是在尝试在目标设备上评估ColorStateList时发生的。因此,如果有任何XML属性与应用程序的minSdk不一致,我会检查颜色定义。通常,执行Lint也可能直接指向这样的错误,因为这非常适合查找这些内容(在命令行上使用./gradlew lint)。

答案 1 :(得分:0)

我找到了解决方案,感谢Thomas Keller对his answer的评论。

基本上,我在v21\style.xml文件中使用了自己的对话框样式:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:alertDialogTheme">@style/MyAlertDialog</item>
    <item name="alertDialogTheme">@style/MyAlertDialog</item>
</style>

<style name="MyAlertDialog" parent="Theme.AppCompat.Dialog.Alert">
    <item name="colorAccent">?attr/colorAccent</item>
</style>

Google API对话框尝试使用此样式,因此将其从我的xml中删除已修复此问题。

但是,我仍然想使用MyAlertDialog(其colorAccent值),所以有人知道我必须添加哪些颜色才能安抚Google API对话框(以及其他任何内容)谷歌/系统对话框)?