null.equals(s) crashes

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));
}

When action==null or type==null, an error is reported: NullPointerException.

Null pointer caused by null.equals - Programmer Sought

Change to 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));
}

Guess you like

Origin blog.csdn.net/sonichty/article/details/130907392