Android 异常之-TextView赋值空指针异常(Null
2020-08-19 本文已影响0人
可乐_JS
1.通过自定义TextView:
package ***;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
/**
* Des: 自定义TextView
* Created by kele on 2020/8/13.
* E-mail:984127585@qq.com
*/
public class KeleTextView extends androidx.appcompat.widget.AppCompatTextView {
public KeleTextView(Context context) {
this(context, null);
}
public KeleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置内容判空处理
*
* @param text
*/
public void setTxt(CharSequence text) {
if (TextUtils.isEmpty(text)) {
return;
}
setText(text, BufferType.NORMAL);
}
}
2.通过工具类:
package ***;
import android.text.TextUtils;
import android.widget.TextView;
/**
* Des: TextView工具类
* Created by kele on 2020/8/13.
* E-mail:984127585@qq.com
*/
public class TextViewUtil {
/**
* 赋值操作
*
* @param tv
* @param text
*/
public static void seText(TextView tv, String text) {
if (null == tv) {
return;
}
if (TextUtils.isEmpty(text)) {
return;
}
tv.setText(text);
}
}