ARouter
ARouter copied to clipboard
添加 getIntent()、getArguments()为空时可能引起 NPE 的判断
1、添加对 getIntent()
、getIntent().getExtras()
、getArguments()
可能为null
的情况进行判断。
最终生成的代码示例如下:
Activity:
android.os.Bundle bundle = substitute.getIntent().getExtras();
Fragment:
android.os.Bundle bundle = substitute.getArguments();
substitute.age = bundle == null ? substitute.age : bundle.getInt("age", substitute.age);
substitute.height = bundle == null ? substitute.height : bundle.getInt("height", substitute.height);
substitute.girl = bundle == null ? substitute.girl : bundle.getBoolean("boy", substitute.girl);
substitute.ch = bundle == null ? substitute.ch : bundle.getChar("ch", substitute.ch);
substitute.fl = bundle == null ? substitute.fl : bundle.getFloat("fl", substitute.fl);
substitute.dou = bundle == null ? substitute.dou : bundle.getDouble("dou", substitute.dou);
substitute.ser = bundle == null ? substitute.ser : (com.alibaba.android.arouter.demo.service.model.TestSerializable) bundle.getSerializable("ser");
substitute.pac = bundle == null ? substitute.pac : (com.alibaba.android.arouter.demo.service.model.TestParcelable) bundle.getParcelable("pac");
2、添加对 OBJECT 类型,使用serializationService.parseObject
时,保留字段默认值的功能。
先把
serializationService.parseObject
的值赋值给"$T default_" + fieldName
,再判断不为null
时,再赋值给"substitute." + fieldName
生成的示例代码:
if (null != serializationService) {
List<TestObj> default_objList = serializationService.parseObject(bundle.getString("objList"), new com.alibaba.android.arouter.facade.model.TypeWrapper<List<TestObj>>(){}.getType());
if (null != default_objList) {
substitute.objList = default_objList;
}
} else {
Log.e("ARouter::", "You want automatic inject the field 'objList' in class 'BlankFragment' , then you should implement 'SerializationService' to support object auto inject!");
}