【问题标题】:Android - java.lang.reflect.InvocationTargetException calling dialog.show()Android - java.lang.reflect.InvocationTargetException 调用 dialog.show()
【发布时间】:2016-09-26 19:47:24
【问题描述】:

尝试在 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;
}

【问题讨论】:

  • 您的 xml 布局不支持某些内容。
  • 发布您的 xml 布局。问题是膨胀 com.android.internal.widget.DialogTitle
  • 我现在已经添加了我的代码。如您所见,我收到了来自GoogleApiAvailability 的错误对话框,所以我对布局文件没有发言权...

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


【解决方案1】:

下面的错误方式告诉你

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

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

【讨论】:

  • 感谢您的帮助。你能给我一些关于如何检查颜色定义以及在哪里/如何使用./gradlew lint 的指导,因为我不熟悉其中任何一个。 (注意 - 我的应用的 minSdkVersion 是 15。)
  • 在底部抽屉的 Android Studio 中打开“终端”窗口。在 Windows 上,你需要输入并执行gradlew.bat lint,在 macOS 和 Linux 上是所说的./gradlew lint 命令。
  • 我在终端中执行了gradlew.bat lint,它出现了 3 个与 twitter4j 依赖项相关的错误,因此我已将其从我的项目中删除,但我仍然遇到相同的异常。但是,我注意到了这个警告:W/GooglePlayServicesUtil: Google Play services out of date. Requires 9683000 but found 9452480 这可能是潜在的问题?
  • 这里有一个讨论:code.google.com/p/android/issues/detail?id=212879。这里有一个建议的解决方案:stackoverflow.com/a/39706752/1617737 - 但它对我不起作用......:-/
  • 实际上,我认为您遇到的问题似乎与支持库有关。看看这是否能解决您的问题:stackoverflow.com/questions/30595187/…
【解决方案2】:

感谢 Thomas Keller 在his answer 上的 cmets,我找到了解决方案。

基本上,我在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 对话框(以及任何其他 Google/系统对话框)?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多