尝试调用虚拟方法' ..'在null对象引用上

时间:2015-02-19 07:49:48

标签: android onresume android-toolbar

当我返回上一个活动时,尝试返回工具栏不透明度恢复正常。我在onResume方法中接受了实现这个的建议:

mActionBarBackgroundDrawable.setAlpha(255);
    mToolbar.setBackground(mActionBarBackgroundDrawable);

但它给了我一个错误: 尝试在空对象引用上调用虚拟方法'android.graphics.drawable.Drawable android.support.v7.widget.Toolbar.getBackground()'。

这是我的代码:     public class CollapsingToolbar extends ActionBarActivity实现OnScrollChangedCallback {

private Toolbar mToolbar;
private Drawable mActionBarBackgroundDrawable;
private View mHeader;
private int mLastDampedScroll;
private int mInitialStatusBarColor;
private int mFinalStatusBarColor;
private SystemBarTintManager mStatusBarManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.toolbar_default);

    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    mActionBarBackgroundDrawable = mToolbar.getBackground();
    setSupportActionBar(mToolbar);

    mStatusBarManager = new SystemBarTintManager(this);
    mStatusBarManager.setStatusBarTintEnabled(true);
    mInitialStatusBarColor = Color.BLACK;
    mFinalStatusBarColor = getResources().getColor(R.color.myPrimaryDarkColor);

    mHeader = findViewById(R.id.header);

    ObservableScrollable scrollView = (ObservableScrollable) findViewById(R.id.scrollview);
    scrollView.setOnScrollChangedCallback(this);

    onScroll(-1, 0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onScroll(int l, int scrollPosition) {
    int headerHeight = mHeader.getHeight() - mToolbar.getHeight();
    float ratio = 0;
    if (scrollPosition > 0 && headerHeight > 0)
        ratio = (float) Math.min(Math.max(scrollPosition, 0), headerHeight) / headerHeight;

    updateActionBarTransparency(ratio);
    updateStatusBarColor(ratio);
    updateParallaxEffect(scrollPosition);
}

private void updateActionBarTransparency(float scrollRatio) {
    int newAlpha = (int) (scrollRatio * 255);
    mActionBarBackgroundDrawable.setAlpha(newAlpha);
    mToolbar.setBackground(mActionBarBackgroundDrawable);
}



private void updateStatusBarColor(float scrollRatio) {
    int r = interpolate(Color.red(mInitialStatusBarColor), Color.red(mFinalStatusBarColor), 1 - scrollRatio);
    int g = interpolate(Color.green(mInitialStatusBarColor), Color.green(mFinalStatusBarColor), 1 - scrollRatio);
    int b = interpolate(Color.blue(mInitialStatusBarColor), Color.blue(mFinalStatusBarColor), 1 - scrollRatio);
    mStatusBarManager.setTintColor(Color.rgb(r, g, b));
}

private void updateParallaxEffect(int scrollPosition) {
    float damping = 0.5f;
    int dampedScroll = (int) (scrollPosition * damping);
    int offset = mLastDampedScroll - dampedScroll;
    mHeader.offsetTopAndBottom(-offset);

    mLastDampedScroll = dampedScroll;
}

private int interpolate(int from, int to, float param) {
    return (int) (from * param + to * (1 - param));
}

@Override
public void onResume(){
    super.onResume();

    mActionBarBackgroundDrawable.setAlpha(255);
    mToolbar.setBackground(mActionBarBackgroundDrawable);
}

}

这是我称之为活动的地方

            public void onClick(View itemView){
                Intent intent = new Intent(itemView.getContext(), CollapsingToolbar.class);
                itemView.getContext().startActivity(intent);
            }
        });

任何sugestions?我是否必须在我之前的活动中实现它?

0 个答案:

没有答案