我建立一個項目。但我有問題。 我的項目有網頁A,網頁BAndroid ViewPager內Layoutinflator
從圖片http://s20.postimg.org/5pupjbuyl/untitled.png
頁A是的FrameLayout(ID =佈局),在FrameLayout裏已經LinearLayout中和的FrameLayout(ID =含量) 頁B是的FrameLayout(ID =尋呼機),在FrameLayout裏有Viewpager
我會採取的FrameLayout(ID =含量)網頁A頁含B的FrameLayout(ID =尋呼機)
(注意:我要儘量減少我的代碼,因爲它的許多行)
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;
}}
我有一個probleam在myPager.setAdapter(P);
Android Report Bug is NullPointerException
如何解決?
謝謝你的回答^^
PS。抱歉,英語。
哦。好..謝謝^^ –
不客氣。如果有幫助,請接受答案。 – Apoorv
嗯..第一次當我移動myPager.setAdapter在getView(): 我認爲這是沒有問題的。 但我添加代碼Content.addView(pager.getView());在PageIndicatorActivity .java中。這是問題 –