Avoid TextInputEditText crash on some Meizu devices
This commit is contained in:
1 parent
195e9a5655
commit
cb706941c5
7 files changed
+69
-13
No files matched your search
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2018 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.support.design.widget.TextInputEditText;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.TextView;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
// Avoid crash on some Meizu devices
|
||||
// https://github.com/android-in-china/Compatibility/issues/11
|
||||
// https://stackoverflow.com/questions/51891415/nullpointerexception-on-meizu-devices-in-editor-updatecursorpositionmz/52001305
|
||||
public class HackyTextInputEditText extends TextInputEditText {
|
||||
|
||||
public HackyTextInputEditText(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getHint() {
|
||||
boolean isMeizu = Build.MANUFACTURER.toLowerCase().contains("meizu");
|
||||
boolean isPreP = Build.VERSION.SDK_INT < Build.VERSION_CODES.P;
|
||||
|
||||
if (isMeizu && isPreP) {
|
||||
try {
|
||||
return getSuperHint();
|
||||
} catch (Exception e) {
|
||||
return super.getHint();
|
||||
}
|
||||
} else {
|
||||
return super.getHint();
|
||||
}
|
||||
}
|
||||
|
||||
public CharSequence getSuperHint() throws NoSuchFieldException, IllegalAccessException {
|
||||
Field field = TextView.class.getDeclaredField("mHint");
|
||||
field.setAccessible(true);
|
||||
return (CharSequence) field.get(this);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user