我正在使用支持libary 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上设置错误。这是通过点击按钮完成BTW我得到了这个例外:
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()的access修饰符未在库中正确设置。但这是我的猜测。我怎样才能摆脱这个异常,因为这个基础会破坏我迁移到app compat库的原因?
答案 0 :(得分:1)
您几乎肯定会在运行时使用与您期望的类不同的类。
按照这个
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>
让我知道它是否适合你。
答案 1 :(得分:0)
它是一个错误,请参阅: https://code.google.com/p/android/issues/detail?id=78377
它应该由24.0.0解决..尝试更新你的android支持并检查该问题是否仍然存在。
如果有稳定的基本解决方案,我强烈建议避免使用任何解决方法。
我希望这可能有所帮助,&#39;。
答案 2 :(得分:0)
你的问题与onclick方法有关...,我不知道也许你应该尝试不同,请更改你的布局并省略Button的onclick属性,并通过完整代码在活动中声明它:
activity_test.xml中的更改按钮
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSetError"
android:text="TextInputLayout.setError()" />
ActivityTest.java
public class ActivityTest extends AppCompatActivity
{
private TextInputLayout txtInput;
private Button btnSetError;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_test);
txtInput = (TextInputLayout) findViewById(R.id.textInputLayout);
btnSetError =(Button)findViewById(R.id.btnSetError);
btnSetError.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtInput.setError("ERROR");
}
});
}