Frage

I build a project. but I'm having problems. My project has page A , Page B

from Picture http://s20.postimg.org/5pupjbuyl/untitled.png

Page A is FrameLayout (id=layout) , in FrameLayout has LinearLayout and FrameLayout (id=content) Page B is FrameLayout (id=pager) , in FrameLayout has Viewpager

I would take Framelayout (id=content) in page A containing FrameLayout(id = pager) in page B

(Notice : I try to minimize my code because It many line)

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;
 }}

and 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;   
}}

I have a probleam at myPager.setAdapter(p);

Android Report Bug is NullPointerException

How to solve?

THANK YOU FOR ANSWER ^^

PS. sorry english .

War es hilfreich?

Lösung

This is happening because your Activity does not have the ViewPager in its layout. Its in the other layout. You will need to do setAdapter in getView()

Andere Tipps

Your current PageIndicatorActivity layout does not have a ViewPager. If you need to continue this way you have to move to Activity(Page B) and follow your implementation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top