导致此异常的原因是什么:java.lang.NoSuchFieldException?
我试图用一些反思来解决问题,使我的文章有一个提纲,但我得到了下面的例外,我无法找出是什么原因造成的 我已经尝试使用AppCompatTextView的超类来查找字段,但它抛出了相同的异常 例外情况导致此异常的原因是什么:java.lang.NoSuchFieldException?,java,android,reflection,Java,Android,Reflection,我试图用一些反思来解决问题,使我的文章有一个提纲,但我得到了下面的例外,我无法找出是什么原因造成的 我已经尝试使用AppCompatTextView的超类来查找字段,但它抛出了相同的异常 例外情况 E/OutlineTextView: OutlineTextView: throwed exception! java.lang.NoSuchFieldException: No field mCurTextColor in class Landroidx/appcompat/widget/A
E/OutlineTextView: OutlineTextView: throwed exception!
java.lang.NoSuchFieldException: No field mCurTextColor in class Landroidx/appcompat/widget/AppCompatTextView; (declaration of 'androidx.appcompat.widget.AppCompatTextView' appears in /data/app/com.notyetmidnight.investigativedetective-2hmFJkII1JGtjt1HtBTEcg==/base.apk)
at java.lang.Class.getDeclaredField(Native Method)
at com.notyetmidnight.investigativedetective.CustomComponents.OutlineTextView.<init>(OutlineTextView.java:40)
at com.notyetmidnight.investigativedetective.CustomComponents.OutlineTextView.<init>(OutlineTextView.java:31)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.notyetmidnight.investigativedetective.Interface.CharacterSelection.CharacterSelectionFragment.onCreateView(CharacterSelectionFragment.java:114)
at com.notyetmidnight.investigativedetective.Interface.CharacterSelection.CharacterSelectionFragment.onCreateView(CharacterSelectionFragment.java:43)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.executePendingTransactions(FragmentManagerImpl.java:183)
at com.notyetmidnight.investigativedetective.MainActivity.swapFragments(MainActivity.java:346)
at com.notyetmidnight.investigativedetective.MainActivity.onEvent(MainActivity.java:365)
at java.lang.reflect.Method.invoke(Native Method)
at org.greenrobot.eventbus.EventBus.invokeSubscriber(EventBus.java:507)
at org.greenrobot.eventbus.EventBus.postToSubscription(EventBus.java:434)
at org.greenrobot.eventbus.EventBus.postSingleEventForEventType(EventBus.java:411)
at org.greenrobot.eventbus.EventBus.postSingleEvent(EventBus.java:384)
at org.greenrobot.eventbus.EventBus.post(EventBus.java:265)
at com.notyetmidnight.investigativedetective.Interface.Menu.MenuFragment.lambda$setOnClickListeners$0(MenuFragment.java:71)
at com.notyetmidnight.investigativedetective.Interface.Menu.-$$Lambda$MenuFragment$gQ5F3e04Bn82QqyI2yepYZlvbR8.onClick(Unknown Source:0)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
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:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/OutlineTextView:OutlineTextView:抛出异常!
java.lang.NoSuchFieldException:类Landroidx/appcompat/widget/AppCompatTextView中没有字段mCurTextColor;(在/data/app/com.notyetmidnight.investigativedetective-2hmFJkII1JGtjt1HtBTEcg==/base.apk中出现“androidx.appcompat.widget.appcompatitextView”的声明)
位于java.lang.Class.getDeclaredField(本机方法)
在com.notyetmidnight.investigativedetective.CustomComponents.OutlineTextView上。(OutlineTextView.java:40)
在com.notyetmidnight.investigativedetective.CustomComponents.OutlineTextView上。(OutlineTextView.java:31)
位于java.lang.reflect.Constructor.newInstance0(本机方法)
位于java.lang.reflect.Constructor.newInstance(Constructor.java:343)
位于android.view.LayoutInflater.createView(LayoutInflater.java:854)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
位于android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
位于android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
位于android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
位于android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
在android.view.LayoutInflater.充气(LayoutInflater.java:682)
在android.view.LayoutInflater.inflate(LayoutInflater.java:534)
位于com.notyetmidnight.investigativedetective.Interface.CharacterSelection.CharacterSelectionFragment.onCreateView(CharacterSelectionFragment.java:114)
位于com.notyetmidnight.investigativedetective.Interface.CharacterSelection.CharacterSelectionFragment.onCreateView(CharacterSelectionFragment.java:43)
位于androidx.fragment.app.fragment.performCreateView(fragment.java:2600)
位于androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
在androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
位于androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
位于androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
位于androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
位于androidx.fragment.app.FragmentManagerImpl.ExecuteOptoGether(FragmentManagerImpl.java:1869)
在androidx.fragment.app.FragmentManagerImpl.RemovedAndTopOperationsAndExecute(FragmentManagerImpl.java:1824)
位于androidx.fragment.app.FragmentManagerImpl.ExecutPendingActions(FragmentManagerImpl.java:1727)
位于androidx.fragment.app.FragmentManagerImpl.executePendingTransactions(FragmentManagerImpl.java:183)
在com.notyetmidnight.investigativedetective.MainActivity.swapFragments(MainActivity.java:346)上
位于com.notyetmidnight.investigativedetective.MainActivity.onEvent(MainActivity.java:365)
位于java.lang.reflect.Method.invoke(本机方法)
位于org.greenrobot.eventbus.eventbus.invokeSubscriber(eventbus.java:507)
位于org.greenrobot.eventbus.eventbus.postToSubscription(eventbus.java:434)
位于org.greenrobot.eventbus.eventbus.postSingleEventForEventType(eventbus.java:411)
位于org.greenrobot.eventbus.eventbus.postSingleEvent(eventbus.java:384)
位于org.greenrobot.eventbus.eventbus.post(eventbus.java:265)
在com.notyetmidnight.investigativedetective.Interface.Menu.menuframent.lambda$setOnClickListeners$0(menuframent.java:71)
在com.notyetmidnight.investigativedetective.Interface.Menu.-$$Lambda$menufpartment$GQ5F3E04BN82QYI2YEPYZLVBR8.onClick(未知来源:0)
在android.view.view.performClick上(view.java:7125)
在android.view.view.performClickInternal(view.java:7102)
在android.view.view.access$3500(view.java:801)
在android.view.view$PerformClick.run(view.java:27336)
位于android.os.Handler.handleCallback(Handler.java:883)
位于android.os.Handler.dispatchMessage(Handler.java:100)
位于android.os.Looper.loop(Looper.java:214)
位于android.app.ActivityThread.main(ActivityThread.java:7356)
位于java.lang.reflect.Method.invoke(本机方法)
位于com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
OutlineTextView
package com.notyetmidnight.investigativedetective.CustomComponents;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.util.Preconditions;
import com.notyetmidnight.investigativedetective.R;
import java.lang.reflect.Field;
public class OutlineTextView extends AppCompatTextView {
private static final String TAG = "OutlineTextView";
private Field colorField;
private int textColor;
private int outlineColor;
public OutlineTextView(Context context) {
this(context, null);
}
public OutlineTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public OutlineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
try {
// Class superClass = Preconditions.checkNotNull(AppCompatTextView.class.getSuperclass());
// colorField = superClass.getDeclaredField("mCurTextColor");
colorField = OutlineTextView.class.getSuperclass().getDeclaredField("mCurTextColor");
colorField.setAccessible(true);
// If the reflection fails (which really shouldn't happen), we
// won't need the rest of this stuff, so we keep it in the try-catch
textColor = getTextColors().getDefaultColor();
// These can be changed to hard-coded default
// values if you don't need to use XML attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView);
outlineColor = a.getColor(R.styleable.OutlineTextView_outlineColor, Color.TRANSPARENT);
setOutlineStrokeWidth(a.getDimensionPixelSize(R.styleable.OutlineTextView_outlineWidth, 0));
a.recycle();
}
catch (NullPointerException | NoSuchFieldException e) {
Log.e(TAG, "OutlineTextView: throwed exception!", e);
colorField = null;
}
}
@Override
public void setTextColor(int color) {
// We want to track this ourselves
// The super call will invalidate()
textColor = color;
super.setTextColor(color);
}
public void setOutlineColor(int color) {
outlineColor = color;
invalidate();
}
public void setOutlineWidth(float width) {
setOutlineStrokeWidth(width);
invalidate();
}
private void setOutlineStrokeWidth(float width) {
getPaint().setStrokeWidth(2 * width + 1);
}
@Override
protected void onDraw(Canvas canvas) {
// If we couldn't get the Field, then we
// need to skip this, and just draw as usual
if (colorField != null) {
// Outline
setColorField(outlineColor);
getPaint().setStyle(Paint.Style.STROKE);
super.onDraw(canvas);
// Reset for text
setColorField(textColor);
getPaint().setStyle(Paint.Style.FILL);
} else {
Log.e(TAG, "onDraw: could not get color field. NullPointerException!");
}
super.onDraw(canvas);
}
private void setColorField(int color) {
// We did the null check in onDraw()
try {
colorField.setInt(this, color);
}
catch (IllegalAccessException | IllegalArgumentException e) {
Log.e(TAG, "OutlineTextView: throwed exception!", e);
}
}
// Optional saved state stuff
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.textColor = textColor;
ss.outlineColor = outlineColor;
ss.outlineWidth = getPaint().getStrokeWidth();
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
textColor = ss.textColor;
outlineColor = ss.outlineColor;
getPaint().setStrokeWidth(ss.outlineWidth);
}
private static class SavedState extends BaseSavedState {
int textColor;
int outlineColor;
float outlineWidth;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
textColor = in.readInt();
outlineColor = in.readInt();
outlineWidth = in.readFloat();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(textColor);
out.writeInt(outlineColor);
out.writeFloat(outlineWidth);
}
public static final Parcelable.Creator<SavedState>
CREATOR = new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}
package com.notyetmidnight.investigativedetective.CustomComponents;
导入android.content.Context;
导入android.content.res.TypedArray;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.os.packet;
导入android.os.Parcelable;
导入android.util.AttributeSet;
导入android.util.Log;
导入androidx.appcompat.widget.AppCompatTextView;
导入androidx.core.util.premissions;
导入com.notyetmidnight.investigativedetective.R;
导入java.lang.reflect.Field;
公共类OutlineTextView扩展了AppCompatTextView{
私有静态最终字符串标记=“OutlineTextView”;
专用场色域;
私有int文本颜色;
私人室内彩绘;
公共大纲视图(上下文){
这个(上下文,空);
}
公共大纲视图(上下文、属性集属性){
这(context、attrs、android.R.attr.textViewStyle);
}
公共大纲视图(上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
试一试{
//类超类=前提条件。checkNotNull(AppCompat
-dontwarn java.lang.ClassValue
-keep class java.lang.ClassValue { *; }
-keepattributes *Annotation*
-keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; }
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keepattributes Signature
-keepattributes *Annotation*
-keep class androidx.appcompat.widget.AppCompatTextView { *** mCurTextColor; }