如何修改啊 有没有知道的 大神 本人新手
在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
Did you write it wrong before
findViewById
就没有找到,后面使用ViewPager
肯定是空的。你得检查一下自己的id
?