2017-10-06 12 views
1

を私のコード(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参照の上setProfileIdを呼び出すことはできません。

どのように私はきちんとTHSビューにアクセスしてください。

答えて

2

あなたが私のためにそれをして、それがうまくいくかどうかを教えてもらえますか?

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この回答を正しくマークし、それがあなたを助けてくれたらそれをアップアップしてください。ありがとう。 –

+0

それは働いていたようです...今すぐ自発的に再び働くことをやめました! –

関連する問題