我看到了tutorial
并且我不确定为什么roboguice不会初始化PhoneDal
?
public class OutgoingCallReceiver extends BroadcastReceiver {
@Inject private CommentDal commentDal;
@Inject private PhoneDal phoneDal;
@Override
public void onReceive(final Context context, final Intent intent) {
Log.v(Constants.LOGGER_TAG, "OutgoingCallReceiver onReceive");
if (intent.getAction()
.equals(OutgoingCallReceiver.OUTGOING_CALL_ACTION)) {
Log.v(Constants.LOGGER_TAG,
"OutgoingCallReceiver NEW_OUTGOING_CALL received");
String phoneNumber = intent.getExtras().getString(
OutgoingCallReceiver.INTENT_PHONE_NUMBER);
if (isBlocked(phoneNumber))
{
setResultData(null);
Intent i = new Intent(context, PopupActivity.class);
i.putExtra(Constants.DIALED_PHONE, phoneDal.getItem(phoneNumber));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
private boolean isBlocked(String phoneNumber) {
Phone phone = phoneDal.getItem(phoneNumber);
boolean ans;
if (phone != null && phone.isBlocked == false)
{
ans = true;
}
else
{
ans = false;
}
return ans;
}
}
在这一行中,它在运行时属于NPE:
Phone phone = phoneDal.getItem(phoneNumber);
我必须先拨打guice.getInjectedElement(type)
吗?