SliderLayout发现ID

时间:2018-02-03 00:49:48

标签: java android android-layout slider navigation-drawer

我遇到了麻烦,我试图在NavigationDrawer中制作SliderLayout。昨天它工作得尽可能好,但是今天我打开android工作室,滑块停止工作,我没有改变代码,现在,它抛出一个NullPointerException异常这就是它所说的:Attempt to invoke virtual method 'void com.daimajia.slider.library.SliderLayout.addSlider(com.daimajia.slider.library.SliderTypes.BaseSliderView)' on a null object reference;

以下是我现在正在使用的代码:

public class News extends NavigationDrawer implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener {

private HashMap<String,String> map;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startUp();
}

private void startUp(){
    setContentView(R.layout.news_navigation_activity);
    onCreateDrawer();
    onSlideShowCreate();
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.getMenu().getItem(0).setChecked(true);

}

private void onSlideShowCreate(){
    map=new HashMap<String,String>();
    SliderLayout slide = findViewById(R.id.slider);
    map.put("Android CupCake", "http://androidblog.esy.es/images/cupcake-1.png");
    map.put("Android Donut", "http://androidblog.esy.es/images/donut-2.png");
    map.put("Android Eclair", "http://androidblog.esy.es/images/eclair-3.png");
    map.put("Android Froyo", "http://androidblog.esy.es/images/froyo-4.png");
    map.put("Android GingerBread", "http://androidblog.esy.es/images/gingerbread-5.png");
    for(String name : map.keySet()){
        TextSliderView textSliderView = new TextSliderView(News.this);
        textSliderView
                .description(name)
                .image(map.get(name))
                .setScaleType(BaseSliderView.ScaleType.Fit)
                .setOnSliderClickListener(this);
        textSliderView.bundle(new Bundle());
        textSliderView.getBundle().putString("extra",name);
        try {
            slide.addSlider(textSliderView); <== Here is where the exception is throwed
        } catch(NullPointerException e){
            System.err.println(e.getLocalizedMessage());
        }

    }

}

所以,我不知道发生了什么,但我知道的是@id/slider来自&#34; news_content_activity.xml&#34;我正在调用&#34; news_nav_activity.xml&#34; (包含&#34; news_content_activity.xml&#34;布局)。所以感谢您的关注和帮助。

更新1 : 就像异常所说的那样,我的变量slidenull;但现在我仍然没有解决方案

1 个答案:

答案 0 :(得分:0)

but what i do know is that the @id/slider is from "news_content_activity.xml" and im calling the "news_nav_activity.xml" (that contains "news_content_activity.xml" layout in)

if you mean in contains -> that you use <include>

Specify the ID in the <include>

   <include
    android:id="@+id/test"
    layout="@layout/news_content_activity" />

Then use two findViewById to access fields in the layout

View testView = findViewById(R.id.test);
SliderLayout slide = testView.findViewById(R.id.slider);