我建立了一个项目。但我遇到了问题。 我的项目有第A页,第B页
来自图片http://s20.postimg.org/5pupjbuyl/untitled.png
Page A是FrameLayout(id = layout),在FrameLayout中有LinearLayout和FrameLayout(id = content) 页面B是FrameLayout(id = pager),在FrameLayout中有Viewpager
我会在页面A中使用Framelayout(id = content),其中包含第B页中的FrameLayout(id = pager)
(注意:我尝试最小化我的代码,因为它很多行)
PageIndicatorActivity .java
public class PageIndicatorActivity extends Activity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
..
..
ViewGroup Content = (ViewGroup)findViewById(R.id.layout);
ViewBookAdapter pager = new ViewBookAdapter(this);
Content.addView(pager.getView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main,menu);
return true;
}}
pager.java
public class Pager extends PagerAdapter {
Activity activity;
private static String content[] = {"file:///android_asset/html/page1.html","file:///android_asset/html/page2.html"};
public Pager(Activity act) {
activity = act;
}
public int getCount() {
return content.length;
}
public Object instantiateItem(View collection, int position) {
WebView view = new WebView(activity);
view.loadUrl(content[position]);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}}
和ViewBookAdapter.java
class ViewBookAdapter {
Activity activity;
private LayoutInflater minflate;
private ViewPager myPager;
public ViewBookAdapter(Activity act){
activity = act;
myPager = (ViewPager)act.findViewById(R.id.viewPager);
Pager p = new Pager(act);
myPager.setAdapter(p); // <--- THIS PROBLEM ---
}
public View getView(){
LayoutInflater minflate = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = minflate.inflate(R.layout.pager, null);
return v;
}}
我在myPager.setAdapter(p)上遇到了问题;
Android Report Bug是NullPointerException
如何解决?
谢谢你回答^^
PS。对不起英文。
答案 0 :(得分:0)
这种情况正在发生,因为Activity
的布局中没有ViewPager
。它在另一个布局中。您需要在setAdapter
getView()
答案 1 :(得分:0)
您当前的PageIndicatorActivity
版面没有ViewPager
。如果您需要继续这种方式,则必须转到Activity
(页面B)并按照您的实施方式进行操作。