null.equals(s) 崩溃

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.e(Thread.currentThread().getStackTrace()[2] + "", action + "");
Log.e(Thread.currentThread().getStackTrace()[2] + "", type + "");
if (action.equals(Intent.ACTION_SEND) && type.equals("text/plain")) {
    editText.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
}

当 action==null 或 type==null 时,报错:NullPointerException。

null.equals引起的空指针_无名小卒菜的博客-CSDN博客

改成 s.equals(null)

Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.e(Thread.currentThread().getStackTrace()[2] + "", action + "");
Log.e(Thread.currentThread().getStackTrace()[2] + "", type + "");
if (Intent.ACTION_SEND.equals(action) && "text/plain".equals(type)) {
    editText.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
}

猜你喜欢

转载自blog.csdn.net/sonichty/article/details/130907392
今日推荐