Android上的null对象引用

时间:2016-03-16 21:41:00

标签: java android dagger-2

public class BaseActivity extends TransitionHelper.BaseActivity implements BaseSliderView.OnSliderClickListener,ViewPagerEx.OnPageChangeListener {
    protected static String BASE_FRAGMENT = "base_fragment";
    public @InjectView(R.id.toolbar) Toolbar toolbar;
    public @InjectView(R.id.material_menu_button) MaterialMenuView homeButton;
    public @InjectView(R.id.toolbar_title) TextView toolbarTitle;
    public @InjectView(R.id.fab) Button fab;
    public @InjectView(R.id.drawerLayout) DrawerLayout drawerLayout;
    public @InjectView(R.id.base_fragment_background) View fragmentBackround;

    private SliderLayout mDemoSlider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResource());//layout

    ButterKnife.inject(this);
    initToolbar();
    initBaseFragment(savedInstanceState);

    mDemoSlider = (SliderLayout)findViewById(R.id.slider);



    HashMap<String,String> url_maps = new HashMap<String, String>();
    url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
    url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
    url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
    url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");

    HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
    file_maps.put("Hannibal",R.drawable.hannibal);
    file_maps.put("Big Bang Theory",R.drawable.bigbang);
    file_maps.put("House of Cards",R.drawable.house);
    file_maps.put("Game of Thrones", R.drawable.game_of_thrones);

    for(String name : file_maps.keySet()){
        TextSliderView textSliderView = new TextSliderView(this);
        // initialize a SliderLayout
        textSliderView
                .description(name)
                .image(file_maps.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);

        //add your extra information
        textSliderView.bundle(new Bundle());
        textSliderView.getBundle()
                .putString("extra",name);

        mDemoSlider.addSlider(textSliderView);
    }
    mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
    mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
    mDemoSlider.setCustomAnimation(new DescriptionAnimation());
    mDemoSlider.setDuration(2000);
    mDemoSlider.addOnPageChangeListener(this);


}

private void initToolbar() {
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setTitle("");
        homeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // onBackPressed();
            }
        });
    }
}

private void initBaseFragment(Bundle savedInstanceState) {
    //apply background bitmap if we have one
    if (getIntent().hasExtra("bitmap_id")) {
        fragmentBackround.setBackground(new BitmapDrawable(getResources(), BitmapUtil.fetchBitmapFromIntent(getIntent())));
    }

    Fragment fragment = null;
    if (savedInstanceState != null) {

        fragment = getFragmentManager().findFragmentByTag(BASE_FRAGMENT);
    }
    if (fragment == null) fragment = getBaseFragment();
    setBaseFragment(fragment);
}

protected int getLayoutResource() {
    return R.layout.activity_base;
};

protected Fragment getBaseFragment() {
    int fragmentResourceId = getIntent().getIntExtra("fragment_resource_id", R.layout.fragment_thing_list);
    switch (fragmentResourceId) {
        case R.layout.fragment_thing_list:
        default:
            return new ThingListFragment();
        case R.layout.fragment_thing_detail:
            Toast.makeText(this,"Aqui estoy",Toast.LENGTH_SHORT).show();
            return ThingDetailFragment.create();
        case R.layout.fragment_overaly:
            return new OverlayFragment();
    }
}

public void setBaseFragment(Fragment fragment) {
    if (fragment == null) return;
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.base_fragment, fragment, BASE_FRAGMENT);
    transaction.commit();
}


private MaterialMenuDrawable.IconState currentIconState;
public boolean animateHomeIcon(MaterialMenuDrawable.IconState iconState) {
    if (currentIconState == iconState) return false;
    currentIconState = iconState;
    homeButton.animateState(currentIconState);
    return true;
}

public void setHomeIcon(MaterialMenuDrawable.IconState iconState) {
    if (currentIconState == iconState) return;
    currentIconState = iconState;
    homeButton.setState(currentIconState);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


@Override
public boolean onBeforeBack() {
    ActivityCompat.finishAfterTransition(this);
    return false;
}

public static BaseActivity of(Activity activity) {
    return (BaseActivity) activity;
}

@Override
protected void onStop() {
    // To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
    mDemoSlider.stopAutoCycle();
    super.onStop();
}

@Override
public void onSliderClick(BaseSliderView slider) {

    if (slider.getBundle().get("extra").equals("Hannibal")){

        Toast.makeText(this,slider.getBundle().get("extra") + "Aqui estoy",Toast.LENGTH_SHORT).show();
    }
    Toast.makeText(this,slider.getBundle().get("extra") + "sLyon",Toast.LENGTH_SHORT).show();

}


@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}

0 个答案:

没有答案