不能编程访问动作栏项目的看法 - Android电子

问题描述:

我的代码(在onCreate()方法:不能编程访问动作栏项目的看法 - Android电子

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参考

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

只是为了检查,如果你不介意,你可以为我做这件事,告诉我它是否有效。

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

嗯......这工程! –

+0

@DennisCallanan你可以请标记这个答案的权利和upvote它,如果它无论如何帮助你。谢谢。 –

+0

它似乎已经工作..现在它自发地停止工作了! –