gpt4 book ai didi

android - TextInputLayout.setError() 抛出异常。为什么以及如何解决?

转载 作者:太空狗 更新时间:2023-10-29 13:55:39 24 4
gpt4 key购买 nike

我正在使用支持库 android-support-v7 Pkg.Revision=23.2.1。为 API 23 编译并在 4.1.2 设备上测试。在尝试实现一些现代 UI 模式时,我遇到了这个问题。

activity_test.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextInputLayout EditText default - error" />

</android.support.design.widget.TextInputLayout>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickButtonSetError"
android:text="TextInputLayout.setError()" />

</LinearLayout>

ActivityTest.java

public class ActivityTest extends AppCompatActivity
{

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_test);
}


public void onClickButtonSetError(View view) {
TextInputLayout til = (TextInputLayout)findViewById(R.id.textInputLayout);
til.setErrorEnabled(true);
til.setError("This is a test error message.");
}
}

如果我尝试通过 setError() 方法在 TextInputLayout 上设置错误。顺便说一下,这是通过单击按钮完成的,我得到了这个异常:

10-06 15:47:55.239: E/AndroidRuntime(5780): FATAL EXCEPTION: main
10-06 15:47:55.239: E/AndroidRuntime(5780): java.lang.IllegalStateException: Could not execute method for android:onClick
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.view.View.performClick(View.java:4232)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.view.View$PerformClick.run(View.java:17298)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.os.Handler.handleCallback(Handler.java:615)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.os.Looper.loop(Looper.java:137)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.app.ActivityThread.main(ActivityThread.java:4921)
10-06 15:47:55.239: E/AndroidRuntime(5780): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:47:55.239: E/AndroidRuntime(5780): at java.lang.reflect.Method.invoke(Method.java:511)
10-06 15:47:55.239: E/AndroidRuntime(5780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
10-06 15:47:55.239: E/AndroidRuntime(5780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
10-06 15:47:55.239: E/AndroidRuntime(5780): at dalvik.system.NativeStart.main(Native Method)
10-06 15:47:55.239: E/AndroidRuntime(5780): Caused by: java.lang.reflect.InvocationTargetException
10-06 15:47:55.239: E/AndroidRuntime(5780): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 15:47:55.239: E/AndroidRuntime(5780): at java.lang.reflect.Method.invoke(Method.java:511)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
10-06 15:47:55.239: E/AndroidRuntime(5780): ... 11 more
10-06 15:47:55.239: E/AndroidRuntime(5780): Caused by: java.lang.IllegalAccessError: tried to access method android.support.v7.widget.AppCompatDrawableManager.getPorterDuffColorFilter:()V from class android.support.design.widget.TextInputLayout
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.support.design.widget.TextInputLayout.updateEditTextBackground(TextInputLayout.java:700)
10-06 15:47:55.239: E/AndroidRuntime(5780): at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:592)
10-06 15:47:55.239: E/AndroidRuntime(5780): at com.example.testandroidsupportv7.ActivityEditText.onClickButtonSetError(ActivityEditText.java:30)
10-06 15:47:55.239: E/AndroidRuntime(5780): ... 14 more

正如我所看到的,原因是 IllegalAccessError,它确实告诉了我很多信息。我猜方法 getPorterDuffColorFilter() 的访问修饰符在库中设置不正确。但这是我的猜测。我怎样才能摆脱这个异常,因为这个 basiclay 打败了我迁移到应用程序兼容库的原因?

最佳答案

几乎可以肯定,您在运行时使用的类版本与您预期的版本不同。

按照这个

Caused by: java.lang.IllegalAccessError: tried to access method android.support.v7.widget.AppCompatDrawableManager.getPorterDuffColorFilter:()V from class android.support.design.widget.TextInputLayout

我为您找到了解决方法。以下是您必须在 xml 中为 textInputLayout 应用的样式。

<style name="TextLabel" parent="TextAppearance.AppCompat">
<item name="android:textColorHint">@color/colorLightText</item>
<item name="android:textSize">14dp</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">@color/colorMediumText</item>
<item name="colorControlActivated">@color/colorAccent</item>
<item name="android:textColorHighlight">@color/COLOR</item>
<item name="android:textColorLink">@color/COLOR</item>

让我知道它是否适合您。

关于android - TextInputLayout.setError() 抛出异常。为什么以及如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com