无法以编程方式访问操作栏项目视图 - Android

Can't access Action Bar item view programatically - Android

我的代码(在onCreate()方法中:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.main_action_bar, null);

    actionBar.setCustomView(v);

    profilePictureView = (ProfilePictureView) v.findViewById(R.id.userProfilePicture);

然后当我尝试以编程方式更改视图时使用:

profilePictureView.setProfileId(null);

我收到错误消息“无法在 null 引用上调用 setProfileId

我应该如何正确访问该视图。

如果您不介意,请帮我做一下,然后告诉我它是否有效。

class YourActivity extends AppCompatActivity{
   //simply use getSupportActionBar() and check
   getSupportActionBar().setDisplayShowCustomEnabled(true);
   LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View v = inflator.inflate(R.layout.main_action_bar, null);

   profilePictureView = (ProfilePictureView) v.findViewById(R.id.userProfilePicture);

   profilePictureView.setProfileId(null);

  // declaring it here because you are setting the whole view in the actionbar now after adding the profile pic and all. So may it is a reason it is not been able to find out the actionbar to be applied on
   getSupportActionBar().setCustomView(v);
}