android - ViewPager出现空指针的错误
伊谢尔伦
伊谢尔伦 2017-04-17 17:39:51
0
1
417

如何修改啊 有没有知道的 大神 本人新手

在MainActivity中viewPager.setAdapter(viewPagerAdapter);
后面设置

viewPager.setCurrentItem(1);

viewPager.setCurrentItem(0);

这两个 都出错误 Caused by: java.lang.NullPointerException

    
public class NewsDetailActivity extends AppCompatActivity{

    private WebView mWebView;
    private boolean isFavourite = false;
    private Stories stories;
    private HotStories hotStories;
    private ViewPager pager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_news);

        Toolbar toolbar = (Toolbar) findViewById(R.id.tb1);
        toolbar.setTitle("享受阅读的乐趣");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.arrow_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mWebView = (WebView) findViewById(R.id.webview);
        setWebView(mWebView);
       pager = (ViewPager) findViewById(R.id.viewpager);
    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                stories = (Stories) getIntent().getSerializableExtra("stories");
                new LoadNewsDetailTask(mWebView).execute(stories.getId());
                isFavourite = DailyZhDB.getInstance(NewsDetailActivity.this).isFavourite(stories);

            }else if (position == 1){
                hotStories= (HotStories) getIntent().getSerializableExtra("hotstories");
                new LoadNewsDetailTask(mWebView).execute(hotStories.getId());
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    }

    private void setWebView(WebView mWebView) {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
    } 
    
     public static void startActivity(Context context, Stories news) {
    if (Utility.checkNetworkConnection(context)) {
        Intent i = new Intent(context, NewsDetailActivity.class);
        i.putExtra("stories", news);
        context.startActivity(i);
    } else {
        Utility.noNetworkAlert(context);
    }
}
public static void startHotActivity(Context context, HotStories news) {
    if (Utility.checkNetworkConnection(context)) {
        Intent i = new Intent(context, NewsDetailActivity.class);
        i.putExtra("hotstories", news);
        context.startActivity(i);
    } else {
        Utility.noNetworkAlert(context);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    if (isFavourite) menu.findItem(R.id.action_favourite).setIcon(R.drawable.fav_active);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_settings:
            break;
        case R.id.action_favourite:
            if (!isFavourite) {
                DailyZhDB.getInstance(this).saveFavourite(stories);
                item.setIcon(R.drawable.fav_active);
                isFavourite = true;
            } else {
                DailyZhDB.getInstance(this).deleteFavourite(stories);
                item.setIcon(R.drawable.fav_normal);
                isFavourite = false;
            }
            break;
    }
    return true;
}

}

主Activity

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
迷茫

Did you write it wrong beforefindViewById就没有找到,后面使用ViewPager肯定是空的。你得检查一下自己的id?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template