0

我建立一个项目。但我遇到了问题。我的项目有页面 A ,页面 B

来自图片http://s20.postimg.org/5pupjbuyl/untitled.png

Page A 是 FrameLayout (id=layout) ,在 FrameLayout 有 LinearLayout 和 FrameLayout (id=content) Page B 是 FrameLayout (id=pager) ,在 FrameLayout 有 Viewpager

我会在页面 A 中使用 Framelayout (id=content),在页面 B 中包含 FrameLayout(id = pager)

(注意:我尽量减少我的代码,因为它很多行)

页面指​​示器活动.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;
}}

寻呼机.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 报告 Bug 为 NullPointerException

怎么解决?

谢谢你的回答^^

PS。对不起英语。

4

2 回答 2

0

发生这种情况是因为您的布局Activity中没有ViewPager。它在其他布局中。你需要setAdaptergetView()

于 2013-11-15T09:21:20.840 回答
0

您当前的PageIndicatorActivity布局没有ViewPager. 如果您需要以这种方式继续,您必须转到Activity(页面 B)并遵循您的实施。

于 2013-11-15T09:31:11.563 回答